I am trying to get the total for the Array in the Object.
I am using print_r to get the following to see whats in it.
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [name] => The Drift Bible [category] => Movie [id] => 227431881228 [created_time] => 2011-02-27T21:41:04+0000 ) [1] => stdClass Object ( [name] => Shooter [category] => Movie [id] => 109671005718938 [created_time] => 2011-02-16T09:18:29+0000 ) [2] => stdClass Object ( [name] => Tron [category] => Movie [id] => 99792857339 [created_time] => 2010-11-29T03:18:06+0000 ))
here is my code to get the information
$movies = json_decode(file_get_contents('https://graph.facebook.com/me/movies?'
.'format=json&access_token=' . $session['access_token']));
Again I just need to get the Array value. Such as 3 in the about example above.
I am very new to this. Any advice will help.
Thanks
Try:
$c = count($movies->data)
//creating facebook_id from facebook_url
$sRequestUrl = 'http://graph'.strstr($a_data['contact_data']['facebook'], '.facebook');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $sRequestUrl
));
$res = curl_exec($curl);
curl_close($curl);
$res = json_decode($res);
id = $res->id;
about = $res->about;
just call the name of the variable key
Related
This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 4 years ago.
So I've got a page with some jSON data I want to gather into arrays with cURL. To be precise, cURL when used from a PHP program, not from the command line.
I can do that part well with the following codeblock below:
$token = 'my_token_here';
$headers = ['Authorization: Bearer ' . $token];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_URL => 'https://my.infrastructure.com/api/v1/accounts/search',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPHEADER => $headers
]);
$resp = curl_exec($curl);
curl_close($curl);
$data = json_decode($resp);
dd($data);
This will return me a 2D array of all the accounts on the page in the above URL, each array containing an 'account' with variables like ID, name, etc.
The printed output, should I run "print_r($data)", is this:
Array ( [0] => stdClass Object ( [id] => 130375 [name] => 3Lakes [domain] => 3lakes.instructure.com [distance] => [authentication_provider] => google ) [1] => stdClass Object ( [id] => 130376 [name] => 3Lakes - Parents [domain] => 3lakes.instructure.com [distance] => [authentication_provider] => canvas ) [2] => stdClass Object ( [id] => 126425 [name] => 95 Percent Group [domain] => 95percentgroup.instructure.com [distance] => [authentication_provider] => ) [3] => stdClass Object ( [id] => 129409 [name] => AACM [domain] => aacm.instructure.com [distance] => [authentication_provider] => canvas ) [4] => stdClass Object ( [id] => 129272 [name] => Aalen [domain] => aalen.instructure.com [distance] => [authentication_provider] => canvas ) [5] => stdClass Object ( [id] => 129572 [name] => Aaron Cohn Middle School - MCSD [domain] => mcsd.instructure.com [distance] => [authentication_provider] => ldap ) [6] => stdClass Object ( [id] => 128124 [name] => Abilene Christian University [domain] => acu.instructure.com [distance] => [authentication_provider] => ) [7] => stdClass Object ( [id] => 127204 [name] => Abilene Christian University Online [domain] => acuonline.instructure.com [distance] => [authentication_provider] => ) [8] => stdClass Object ( [id] => 130398 [name] => Abington Public Schools [domain] => abington.instructure.com [distance] => [authentication_provider] => canvas ) [9] => stdClass Object ( [id] => 128797 [name] => Academia Virtual [domain] => canvas.academiavirtual.cr [distance] => [authentication_provider] => ) )
Now, instead of doing all that, I just want to collect a single array, where that array has an ID equal to an ID I give it; i.e if I want to find the ID "15", it returns either the array matching that, or null.
I am not sure how, though. I know that, after the query, I can do PHP to comb the retrieved $data and check each, but I want to do this a bit smoother, presumably in the curl_setopt_array area somehow.
Is what I'm trying to do possible in this way? Any advice?
Your confusing some things here.
cURL has no effect on the data format that is transported because its a library for networking.
You already got the solution, looping over the array and restructure the data to your needs.
Also you dont have a 2D array but an array of stdClass objects.
So first of all if you want to have an 2d array you should pass true as the 2nd argument to json_decode to make it parse JSON-objects into associative arrays instead of stdClass.
Then you use a foreach loop and map your entries by id:
$mappedData = [];
foreach ($data as $item) {
$mappedData[$item->id] = $item; //Or $mappedData[$item["id"]] if assoc
}
There may be some fancy callback methods using array_map etc. but the foreach loop is the simplest (in my opinion).
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I'm trying to loop through an array of moves for a Pokemon website. I'm using the API PokeApi (https://pokeapi.co/).
My question is how can I access the moves in these arrays using plain PHP.
I've tried using this just to call 1 move. But I don't know how to access data that's in arrays. Like "move->version-group-details".
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
echo $pokemon->moves[0];
Thanks in advance :)
So you have two methods here you can do so when I run
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
print_r($pokemon->moves[0]);
I get the result:
stdClass Object
(
[move] => stdClass Object
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
If you want to access a moves name you will have to run $pokemon->moves[0]->move->name since we are getting an object returned. If you want to get the name inside the move_learn_method of version_group_details you will have to run
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
Alternatively, if you want to return all arrays instead of objects just run this
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data, true);
print_r($pokemon['moves'][0]);
This will now return
Array
(
[move] => Array
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
So instead of having to use the object accessor -> you can access the data by using array notation so instead of
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
you can now use:
$pokemon['moves']['version_group_details'][0]['move_learn_method']['name']
Hope that helped.
I have a XML format file which has objects inside objects. This is how I get values of first object, which works fine for me:
$soapclient = new SoapClient('http://anywb/book.asmx?WSDL');
$params = array('ISBN' => "1111");
$response = $soapclient->GetBookByISBN($params);
//This will give me the value "Success"
$result = $response->GetBookByISBNResult->ResponseText;
Now my question is how to access the object which is inside object. For example, how to get "BookID" which is 4 and how to get value of "Type" which is 1?
Any suggestion would be appreciated. Here is the object:
stdClass Object
(
[GetBookByISBN] => stdClass Object
(
[ResponseText] => Success
[SearchResult] => stdClass Object
(
[Search] => Array
(
[0] => stdClass Object
(
[Date] => 2015-10-20
[BookID] => 4
[Discription] => stdClass Object
(
[Type] => 1
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
[1] => stdClass Object
(
[Date] => 2016-11-15
[BookID] => 5
[Discription] => stdClass Object
(
[Type] => 2
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
))))
You do it like this:
$response->GetBookByISBN->SearchResult->Search[0]->BookID;
$response->GetBookByISBN->SearchResult->Search[0]->Discription->Type;
If you would like to have an array containing all book ids you would have something like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = $value->BookID;
}
Or if you would like to be able to modify the values in $response using the $BookIDs (as an example) iot would look like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = &$value->BookID;
}
I m using codeigniter, in my view file
<?php
$app_id = "APP_ID_HERE_XXXXXXXX";
$secret = "SECRET_XXXXXXXX";
$data = file_get_contents('https://graph.facebook.com/me?fields=friends&access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$data = json_decode($data);
}
?>
Problem:How can i print friend list from JSON data
File Data After Decode Like This:
stdClass Object
(
[friends] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => frnd_name
[id] => frnd_id
)
[1] => stdClass Object
(
[name] => frnd_name
[id] => frnd_id
)
[2] => stdClass Object
(
[name] => frnd_name
[id] => frnd_id
)....
How can i print frnds name in list items..Thanx
Try this:
foreach($data->friends->data as $friend){
echo $friend->name." ".$friend->id."<br/>";
}
I have the following PHP code:
define('TOKEN_FILE', 'fb_page_accestoken.txt'); // dont need to change this
$fb = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
'cookie' => true,
));
$access_token = file_get_contents('fb_app_token.txt');
$response = "https://graph.facebook.com/me/accounts?access_token=".$access_token;
$responsez = json_decode(file_get_contents($response ,true));
print_r($responsez);
When I use the above code I decode the JSON string and get the following:
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [category] => Media/news/publishing [name] => PAGE ABC [access_token] => abcdefghijklmnopqrstuvwxyz1234567890 [perms] => Array ( [0] => ADMINISTER [1] => EDIT_PROFILE [2] => CREATE_CONTENT [3] => MODERATE_CONTENT [4] => CREATE_ADS [5] => BASIC_ADMIN ) [id] => 35645645678735 ) [1] => stdClass Object ( [category] => Entertainment website [name] => Page 123 [access_token] => abcdefghijklmnopqrstuvwxyz1234567890 [perms] => Array ( [0] => ADMINISTER [1] => EDIT_PROFILE [2] => CREATE_CONTENT [3] => MODERATE_CONTENT [4] => CREATE_ADS [5] => BASIC_ADMIN ) [id] => 1364564569777454 )
How can I get the above into a PHP array? For example to be able to extract the access_token of page id 1364564569777454.
responsez = json_decode(file_get_contents($response), true);
The second param in json_decode allow convert returned object into associative arrays.
$arr = array();
foreach($responsez->data as $obj){
$arr[] = array("page_id"=>$obj->id,"access _token"=>$obj->access_token);
}
print_r($arr);
I was having troubles with the accounts JSON and this worked better for me:
$request = (new FacebookRequest($session, 'GET', '/' . $this->userID . '/accounts'))->execute();
$graphObject = json_decode($request->getRawResponse(), true);
foreach ($graphObject['data'] as $page) { // array['data', 'paging']
if ($page['id'] == $this->fbAppPageID) {
$this->pageToken = $page['access_token'];
}
}
return $this->pageToken;