separate key value from facebook token arrayCreate ArrayList from arrayPHP: Delete an element from an arrayHow do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueDetermine whether an array contains a valueCheck if a value exists in an array in RubyHow do I remove a particular element from an array in JavaScript?PHP array delete by value (not key)Copy array by valueRemove duplicate values from JS array
Modeling an IP Address
Is it unprofessional to ask if a job posting on GlassDoor is real?
Character reincarnated...as a snail
What would happen to a modern skyscraper if it rains micro blackholes?
How does quantile regression compare to logistic regression with the variable split at the quantile?
LaTeX: Why are digits allowed in environments, but forbidden in commands?
Perform and show arithmetic with LuaLaTeX
How do I deal with an unproductive colleague in a small company?
What are these boxed doors outside store fronts in New York?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Why are electrically insulating heatsinks so rare? Is it just cost?
Why do I get two different answers for this counting problem?
What does "Puller Prush Person" mean?
Why can't we play rap on piano?
What is a clear way to write a bar that has an extra beat?
meaning of に in 本当に?
What does it mean to describe someone as a butt steak?
Paid for article while in US on F-1 visa?
Watching something be written to a file live with tail
Roll the carpet
Why is 150k or 200k jobs considered good when there's 300k+ births a month?
Java Casting: Java 11 throws LambdaConversionException while 1.8 does not
How can I make my BBEG immortal short of making them a Lich or Vampire?
How much of data wrangling is a data scientist's job?
separate key value from facebook token array
Create ArrayList from arrayPHP: Delete an element from an arrayHow do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueDetermine whether an array contains a valueCheck if a value exists in an array in RubyHow do I remove a particular element from an array in JavaScript?PHP array delete by value (not key)Copy array by valueRemove duplicate values from JS array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to access facebook login
without sdk
with php
that's why I get access_token from facebook
like below:
array(1) [""access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581"]=> string(0) ""
but i want only access_token => "xxxxxxxxxxx"
like this $access_token['access_token'];
i already try like :
if(isset($access_token['access_token']) || array_key_exists('access_token', $access_token))
or
parse_str($access_token, $token_array);
but no luck!!
How could I do this?
public function facebookAction()
if(isset($_REQUEST["error"]))
$this->flashSession->error("ERROR:: ".$_REQUEST['error']);
return $this->response->redirect('index');
elseif(isset($_REQUEST["code"]))
if(isset($_REQUEST["state"]) && $_REQUEST["state"]==$_SESSION["state"])
$access_token = $this->get_access_token();
//[ Problem is here ]
$raw = file_get_contents("https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=".$access_token);
$data_array = json_decode($raw,TRUE);
$fbid = $data_array['id'];
$fbmail = $data_array['email'];
$this->session->set('id', '1000');
$this->session->set("uname", $fbmail);
$this->flashSession->success("SUCCESS:: You are now flying with Phalcon!");
return $this->response->redirect('index');
else $this->flashSession->error("ERROR:: Request STATE & CODE Error!");return $this->response->redirect('index');
else $this->dialog();
function generate_state() return md5(uniqid(rand(),TRUE));
function dialog()
$app_secret = "bbbbbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaa";
//$auth_type = 'rerequest';
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$fbPermission = array('email');
$defaultGraphVersion = 'v3.2';
$state = $this->generate_state();
$_SESSION["state"] = $state;
return $this->response->redirect('https://www.facebook.com/'.$defaultGraphVersion.'/dialog/oauth?client_id='.$app_id.'&scope=email&redirect_uri='.$redirect_uri.'&state='.$state);
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
json_decode($response, $access_token);
return $access_token;
php arrays phalcon
add a comment |
I'm trying to access facebook login
without sdk
with php
that's why I get access_token from facebook
like below:
array(1) [""access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581"]=> string(0) ""
but i want only access_token => "xxxxxxxxxxx"
like this $access_token['access_token'];
i already try like :
if(isset($access_token['access_token']) || array_key_exists('access_token', $access_token))
or
parse_str($access_token, $token_array);
but no luck!!
How could I do this?
public function facebookAction()
if(isset($_REQUEST["error"]))
$this->flashSession->error("ERROR:: ".$_REQUEST['error']);
return $this->response->redirect('index');
elseif(isset($_REQUEST["code"]))
if(isset($_REQUEST["state"]) && $_REQUEST["state"]==$_SESSION["state"])
$access_token = $this->get_access_token();
//[ Problem is here ]
$raw = file_get_contents("https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=".$access_token);
$data_array = json_decode($raw,TRUE);
$fbid = $data_array['id'];
$fbmail = $data_array['email'];
$this->session->set('id', '1000');
$this->session->set("uname", $fbmail);
$this->flashSession->success("SUCCESS:: You are now flying with Phalcon!");
return $this->response->redirect('index');
else $this->flashSession->error("ERROR:: Request STATE & CODE Error!");return $this->response->redirect('index');
else $this->dialog();
function generate_state() return md5(uniqid(rand(),TRUE));
function dialog()
$app_secret = "bbbbbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaa";
//$auth_type = 'rerequest';
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$fbPermission = array('email');
$defaultGraphVersion = 'v3.2';
$state = $this->generate_state();
$_SESSION["state"] = $state;
return $this->response->redirect('https://www.facebook.com/'.$defaultGraphVersion.'/dialog/oauth?client_id='.$app_id.'&scope=email&redirect_uri='.$redirect_uri.'&state='.$state);
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
json_decode($response, $access_token);
return $access_token;
php arrays phalcon
1
How are you creating the array? The array element is an array containing a JSON string, not a decoded object.
– Barmar
Mar 9 at 1:18
It's even weirder. The JSON string is the key of an associative array.
– Barmar
Mar 9 at 1:20
3
You need to show the code that's creating$access_token
, you have some problems there.
– Barmar
Mar 9 at 1:20
I updated my code!! if i add "file_get_contents("graph.facebook.com/…);" like this its working perfectly but i need $access_token['access_token']
– Styled Bee
Mar 9 at 2:32
add a comment |
I'm trying to access facebook login
without sdk
with php
that's why I get access_token from facebook
like below:
array(1) [""access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581"]=> string(0) ""
but i want only access_token => "xxxxxxxxxxx"
like this $access_token['access_token'];
i already try like :
if(isset($access_token['access_token']) || array_key_exists('access_token', $access_token))
or
parse_str($access_token, $token_array);
but no luck!!
How could I do this?
public function facebookAction()
if(isset($_REQUEST["error"]))
$this->flashSession->error("ERROR:: ".$_REQUEST['error']);
return $this->response->redirect('index');
elseif(isset($_REQUEST["code"]))
if(isset($_REQUEST["state"]) && $_REQUEST["state"]==$_SESSION["state"])
$access_token = $this->get_access_token();
//[ Problem is here ]
$raw = file_get_contents("https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=".$access_token);
$data_array = json_decode($raw,TRUE);
$fbid = $data_array['id'];
$fbmail = $data_array['email'];
$this->session->set('id', '1000');
$this->session->set("uname", $fbmail);
$this->flashSession->success("SUCCESS:: You are now flying with Phalcon!");
return $this->response->redirect('index');
else $this->flashSession->error("ERROR:: Request STATE & CODE Error!");return $this->response->redirect('index');
else $this->dialog();
function generate_state() return md5(uniqid(rand(),TRUE));
function dialog()
$app_secret = "bbbbbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaa";
//$auth_type = 'rerequest';
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$fbPermission = array('email');
$defaultGraphVersion = 'v3.2';
$state = $this->generate_state();
$_SESSION["state"] = $state;
return $this->response->redirect('https://www.facebook.com/'.$defaultGraphVersion.'/dialog/oauth?client_id='.$app_id.'&scope=email&redirect_uri='.$redirect_uri.'&state='.$state);
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
json_decode($response, $access_token);
return $access_token;
php arrays phalcon
I'm trying to access facebook login
without sdk
with php
that's why I get access_token from facebook
like below:
array(1) [""access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581"]=> string(0) ""
but i want only access_token => "xxxxxxxxxxx"
like this $access_token['access_token'];
i already try like :
if(isset($access_token['access_token']) || array_key_exists('access_token', $access_token))
or
parse_str($access_token, $token_array);
but no luck!!
How could I do this?
public function facebookAction()
if(isset($_REQUEST["error"]))
$this->flashSession->error("ERROR:: ".$_REQUEST['error']);
return $this->response->redirect('index');
elseif(isset($_REQUEST["code"]))
if(isset($_REQUEST["state"]) && $_REQUEST["state"]==$_SESSION["state"])
$access_token = $this->get_access_token();
//[ Problem is here ]
$raw = file_get_contents("https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=".$access_token);
$data_array = json_decode($raw,TRUE);
$fbid = $data_array['id'];
$fbmail = $data_array['email'];
$this->session->set('id', '1000');
$this->session->set("uname", $fbmail);
$this->flashSession->success("SUCCESS:: You are now flying with Phalcon!");
return $this->response->redirect('index');
else $this->flashSession->error("ERROR:: Request STATE & CODE Error!");return $this->response->redirect('index');
else $this->dialog();
function generate_state() return md5(uniqid(rand(),TRUE));
function dialog()
$app_secret = "bbbbbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaa";
//$auth_type = 'rerequest';
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$fbPermission = array('email');
$defaultGraphVersion = 'v3.2';
$state = $this->generate_state();
$_SESSION["state"] = $state;
return $this->response->redirect('https://www.facebook.com/'.$defaultGraphVersion.'/dialog/oauth?client_id='.$app_id.'&scope=email&redirect_uri='.$redirect_uri.'&state='.$state);
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
json_decode($response, $access_token);
return $access_token;
php arrays phalcon
php arrays phalcon
edited Mar 9 at 2:49
Styled Bee
asked Mar 9 at 1:08
Styled BeeStyled Bee
239
239
1
How are you creating the array? The array element is an array containing a JSON string, not a decoded object.
– Barmar
Mar 9 at 1:18
It's even weirder. The JSON string is the key of an associative array.
– Barmar
Mar 9 at 1:20
3
You need to show the code that's creating$access_token
, you have some problems there.
– Barmar
Mar 9 at 1:20
I updated my code!! if i add "file_get_contents("graph.facebook.com/…);" like this its working perfectly but i need $access_token['access_token']
– Styled Bee
Mar 9 at 2:32
add a comment |
1
How are you creating the array? The array element is an array containing a JSON string, not a decoded object.
– Barmar
Mar 9 at 1:18
It's even weirder. The JSON string is the key of an associative array.
– Barmar
Mar 9 at 1:20
3
You need to show the code that's creating$access_token
, you have some problems there.
– Barmar
Mar 9 at 1:20
I updated my code!! if i add "file_get_contents("graph.facebook.com/…);" like this its working perfectly but i need $access_token['access_token']
– Styled Bee
Mar 9 at 2:32
1
1
How are you creating the array? The array element is an array containing a JSON string, not a decoded object.
– Barmar
Mar 9 at 1:18
How are you creating the array? The array element is an array containing a JSON string, not a decoded object.
– Barmar
Mar 9 at 1:18
It's even weirder. The JSON string is the key of an associative array.
– Barmar
Mar 9 at 1:20
It's even weirder. The JSON string is the key of an associative array.
– Barmar
Mar 9 at 1:20
3
3
You need to show the code that's creating
$access_token
, you have some problems there.– Barmar
Mar 9 at 1:20
You need to show the code that's creating
$access_token
, you have some problems there.– Barmar
Mar 9 at 1:20
I updated my code!! if i add "file_get_contents("graph.facebook.com/…);" like this its working perfectly but i need $access_token['access_token']
– Styled Bee
Mar 9 at 2:32
I updated my code!! if i add "file_get_contents("graph.facebook.com/…);" like this its working perfectly but i need $access_token['access_token']
– Styled Bee
Mar 9 at 2:32
add a comment |
2 Answers
2
active
oldest
votes
in get_access_token
change parse_str
to json_decode
;
parse_str('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', $res);
print_r($res);
Output
Array
(
["access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581] =>
)
Sandbox
Parse string just takes the string and treats it like this, similar to if it was part of a URL query string example.com?foo
:
parse_str('foo=bar', $res);
print_r($res);
parse_str('foo', $res);
print_r($res);
Output
//parse_str('foo=bar', $res);
Array
(
[foo] => bar
)
//parse_str('foo', $res);
Array
(
[foo] =>
)
And just as foo
would become the key in [foo => bar]
your string become the key up there.
parse_str Parses encoded_string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).
Using Json Decode
print_r(json_decode('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', true));
Output
Array
(
[access_token] => xxxxxxxxxxxxxxx
[token_type] => bearer
[expires_in] => 543543581
)
So:
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
$res = json_decode($response, true);
//return false or the access_token
return isset($res['access_token']) ? $res['access_token'] : false;
//OR
return $res; //return the whole response
//---------- old code ---------
//parse_str($response, $access_token);
//return $access_token;
updated as your instruction. i just get "xxxxxxxxxx" value by: foreach ($access_token as $key => $value) echo "$key = $value<br/>"; and then i manually copy the value but not working with $access_token
– Styled Bee
Mar 9 at 2:50
1
You only have one access token,$access_token['access_token']
or you can just return that fromget_access_token
– ArtisticPhoenix
Mar 9 at 2:52
Thanx So Much!! Gr8 Job my Friend!! It's Working as expected.
– Styled Bee
Mar 9 at 2:54
1
Kool, it was pretty obvious when I sawparse_str
as that is used for query string data not JSON.
– ArtisticPhoenix
Mar 9 at 2:56
add a comment |
So you are getting response as array item, where key is your JSON data and empty value?
Your response is ugly, but this is how you would have grabbed your value:
<?php
$response = [
'"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581' => '',
];
$response_key = array_keys($response)[0];
$response_array = json_decode($response_key, true);
$access_token = $response_array['access_token'];
echo $access_token;
?>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073020%2fseparate-key-value-from-facebook-token-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
in get_access_token
change parse_str
to json_decode
;
parse_str('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', $res);
print_r($res);
Output
Array
(
["access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581] =>
)
Sandbox
Parse string just takes the string and treats it like this, similar to if it was part of a URL query string example.com?foo
:
parse_str('foo=bar', $res);
print_r($res);
parse_str('foo', $res);
print_r($res);
Output
//parse_str('foo=bar', $res);
Array
(
[foo] => bar
)
//parse_str('foo', $res);
Array
(
[foo] =>
)
And just as foo
would become the key in [foo => bar]
your string become the key up there.
parse_str Parses encoded_string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).
Using Json Decode
print_r(json_decode('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', true));
Output
Array
(
[access_token] => xxxxxxxxxxxxxxx
[token_type] => bearer
[expires_in] => 543543581
)
So:
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
$res = json_decode($response, true);
//return false or the access_token
return isset($res['access_token']) ? $res['access_token'] : false;
//OR
return $res; //return the whole response
//---------- old code ---------
//parse_str($response, $access_token);
//return $access_token;
updated as your instruction. i just get "xxxxxxxxxx" value by: foreach ($access_token as $key => $value) echo "$key = $value<br/>"; and then i manually copy the value but not working with $access_token
– Styled Bee
Mar 9 at 2:50
1
You only have one access token,$access_token['access_token']
or you can just return that fromget_access_token
– ArtisticPhoenix
Mar 9 at 2:52
Thanx So Much!! Gr8 Job my Friend!! It's Working as expected.
– Styled Bee
Mar 9 at 2:54
1
Kool, it was pretty obvious when I sawparse_str
as that is used for query string data not JSON.
– ArtisticPhoenix
Mar 9 at 2:56
add a comment |
in get_access_token
change parse_str
to json_decode
;
parse_str('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', $res);
print_r($res);
Output
Array
(
["access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581] =>
)
Sandbox
Parse string just takes the string and treats it like this, similar to if it was part of a URL query string example.com?foo
:
parse_str('foo=bar', $res);
print_r($res);
parse_str('foo', $res);
print_r($res);
Output
//parse_str('foo=bar', $res);
Array
(
[foo] => bar
)
//parse_str('foo', $res);
Array
(
[foo] =>
)
And just as foo
would become the key in [foo => bar]
your string become the key up there.
parse_str Parses encoded_string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).
Using Json Decode
print_r(json_decode('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', true));
Output
Array
(
[access_token] => xxxxxxxxxxxxxxx
[token_type] => bearer
[expires_in] => 543543581
)
So:
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
$res = json_decode($response, true);
//return false or the access_token
return isset($res['access_token']) ? $res['access_token'] : false;
//OR
return $res; //return the whole response
//---------- old code ---------
//parse_str($response, $access_token);
//return $access_token;
updated as your instruction. i just get "xxxxxxxxxx" value by: foreach ($access_token as $key => $value) echo "$key = $value<br/>"; and then i manually copy the value but not working with $access_token
– Styled Bee
Mar 9 at 2:50
1
You only have one access token,$access_token['access_token']
or you can just return that fromget_access_token
– ArtisticPhoenix
Mar 9 at 2:52
Thanx So Much!! Gr8 Job my Friend!! It's Working as expected.
– Styled Bee
Mar 9 at 2:54
1
Kool, it was pretty obvious when I sawparse_str
as that is used for query string data not JSON.
– ArtisticPhoenix
Mar 9 at 2:56
add a comment |
in get_access_token
change parse_str
to json_decode
;
parse_str('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', $res);
print_r($res);
Output
Array
(
["access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581] =>
)
Sandbox
Parse string just takes the string and treats it like this, similar to if it was part of a URL query string example.com?foo
:
parse_str('foo=bar', $res);
print_r($res);
parse_str('foo', $res);
print_r($res);
Output
//parse_str('foo=bar', $res);
Array
(
[foo] => bar
)
//parse_str('foo', $res);
Array
(
[foo] =>
)
And just as foo
would become the key in [foo => bar]
your string become the key up there.
parse_str Parses encoded_string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).
Using Json Decode
print_r(json_decode('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', true));
Output
Array
(
[access_token] => xxxxxxxxxxxxxxx
[token_type] => bearer
[expires_in] => 543543581
)
So:
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
$res = json_decode($response, true);
//return false or the access_token
return isset($res['access_token']) ? $res['access_token'] : false;
//OR
return $res; //return the whole response
//---------- old code ---------
//parse_str($response, $access_token);
//return $access_token;
in get_access_token
change parse_str
to json_decode
;
parse_str('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', $res);
print_r($res);
Output
Array
(
["access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581] =>
)
Sandbox
Parse string just takes the string and treats it like this, similar to if it was part of a URL query string example.com?foo
:
parse_str('foo=bar', $res);
print_r($res);
parse_str('foo', $res);
print_r($res);
Output
//parse_str('foo=bar', $res);
Array
(
[foo] => bar
)
//parse_str('foo', $res);
Array
(
[foo] =>
)
And just as foo
would become the key in [foo => bar]
your string become the key up there.
parse_str Parses encoded_string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).
Using Json Decode
print_r(json_decode('"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581', true));
Output
Array
(
[access_token] => xxxxxxxxxxxxxxx
[token_type] => bearer
[expires_in] => 543543581
)
So:
function get_access_token()
$app_secret = "bbbbbbbbbbbbb";
$app_id = "aaaaaaaaaaaaaa";
$redirect_uri = urlencode("https://localhost/firefly/Oauth/facebook");
$code = $_REQUEST["code"];
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$redirect_uri."&client_secret=".$app_secret."&code=".$code);
$res = json_decode($response, true);
//return false or the access_token
return isset($res['access_token']) ? $res['access_token'] : false;
//OR
return $res; //return the whole response
//---------- old code ---------
//parse_str($response, $access_token);
//return $access_token;
edited Mar 9 at 2:50
answered Mar 9 at 2:38
ArtisticPhoenixArtisticPhoenix
18.3k11226
18.3k11226
updated as your instruction. i just get "xxxxxxxxxx" value by: foreach ($access_token as $key => $value) echo "$key = $value<br/>"; and then i manually copy the value but not working with $access_token
– Styled Bee
Mar 9 at 2:50
1
You only have one access token,$access_token['access_token']
or you can just return that fromget_access_token
– ArtisticPhoenix
Mar 9 at 2:52
Thanx So Much!! Gr8 Job my Friend!! It's Working as expected.
– Styled Bee
Mar 9 at 2:54
1
Kool, it was pretty obvious when I sawparse_str
as that is used for query string data not JSON.
– ArtisticPhoenix
Mar 9 at 2:56
add a comment |
updated as your instruction. i just get "xxxxxxxxxx" value by: foreach ($access_token as $key => $value) echo "$key = $value<br/>"; and then i manually copy the value but not working with $access_token
– Styled Bee
Mar 9 at 2:50
1
You only have one access token,$access_token['access_token']
or you can just return that fromget_access_token
– ArtisticPhoenix
Mar 9 at 2:52
Thanx So Much!! Gr8 Job my Friend!! It's Working as expected.
– Styled Bee
Mar 9 at 2:54
1
Kool, it was pretty obvious when I sawparse_str
as that is used for query string data not JSON.
– ArtisticPhoenix
Mar 9 at 2:56
updated as your instruction. i just get "xxxxxxxxxx" value by: foreach ($access_token as $key => $value) echo "$key = $value<br/>"; and then i manually copy the value but not working with $access_token
– Styled Bee
Mar 9 at 2:50
updated as your instruction. i just get "xxxxxxxxxx" value by: foreach ($access_token as $key => $value) echo "$key = $value<br/>"; and then i manually copy the value but not working with $access_token
– Styled Bee
Mar 9 at 2:50
1
1
You only have one access token,
$access_token['access_token']
or you can just return that from get_access_token
– ArtisticPhoenix
Mar 9 at 2:52
You only have one access token,
$access_token['access_token']
or you can just return that from get_access_token
– ArtisticPhoenix
Mar 9 at 2:52
Thanx So Much!! Gr8 Job my Friend!! It's Working as expected.
– Styled Bee
Mar 9 at 2:54
Thanx So Much!! Gr8 Job my Friend!! It's Working as expected.
– Styled Bee
Mar 9 at 2:54
1
1
Kool, it was pretty obvious when I saw
parse_str
as that is used for query string data not JSON.– ArtisticPhoenix
Mar 9 at 2:56
Kool, it was pretty obvious when I saw
parse_str
as that is used for query string data not JSON.– ArtisticPhoenix
Mar 9 at 2:56
add a comment |
So you are getting response as array item, where key is your JSON data and empty value?
Your response is ugly, but this is how you would have grabbed your value:
<?php
$response = [
'"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581' => '',
];
$response_key = array_keys($response)[0];
$response_array = json_decode($response_key, true);
$access_token = $response_array['access_token'];
echo $access_token;
?>
add a comment |
So you are getting response as array item, where key is your JSON data and empty value?
Your response is ugly, but this is how you would have grabbed your value:
<?php
$response = [
'"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581' => '',
];
$response_key = array_keys($response)[0];
$response_array = json_decode($response_key, true);
$access_token = $response_array['access_token'];
echo $access_token;
?>
add a comment |
So you are getting response as array item, where key is your JSON data and empty value?
Your response is ugly, but this is how you would have grabbed your value:
<?php
$response = [
'"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581' => '',
];
$response_key = array_keys($response)[0];
$response_array = json_decode($response_key, true);
$access_token = $response_array['access_token'];
echo $access_token;
?>
So you are getting response as array item, where key is your JSON data and empty value?
Your response is ugly, but this is how you would have grabbed your value:
<?php
$response = [
'"access_token":"xxxxxxxxxxxxxxx","token_type":"bearer","expires_in":543543581' => '',
];
$response_key = array_keys($response)[0];
$response_array = json_decode($response_key, true);
$access_token = $response_array['access_token'];
echo $access_token;
?>
edited Mar 19 at 23:53
answered Mar 9 at 3:03
HelpNeederHelpNeeder
3,2212067130
3,2212067130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073020%2fseparate-key-value-from-facebook-token-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How are you creating the array? The array element is an array containing a JSON string, not a decoded object.
– Barmar
Mar 9 at 1:18
It's even weirder. The JSON string is the key of an associative array.
– Barmar
Mar 9 at 1:20
3
You need to show the code that's creating
$access_token
, you have some problems there.– Barmar
Mar 9 at 1:20
I updated my code!! if i add "file_get_contents("graph.facebook.com/…);" like this its working perfectly but i need $access_token['access_token']
– Styled Bee
Mar 9 at 2:32