I am trying to use php to parse a JSON feed of posts using Facebook Graph API
I found the following solution for comments...
<?php
$request_url ="https://graph.facebook.com/comments/?
ids=http://www.youtube.com/watch?v=fyF-fj-1coY&feature=player_embedded";
$requests = file_get_contents($request_url);
$fb_response = json_decode($requests);
foreach ($fb_response as $key => $response) {
foreach ($fb_response->$key as $data) {
foreach ($data as $item) {
echo 'NAME: ' . $item->name . '<br />';
echo 'From ID: ' . $item->from->id . '<br />';
echo 'From Name: ' . $item->from->name . '<br />';
echo 'Message: ' . $item->message . '<br />';
echo 'Timestamp: ' . $item->created_time . '<br /><br />';
}
}
}
?>
This is the url id I'm working with: https://graph.facebook.com/210849652406/feed/?access_token={VALID_USER_TOKEN}
I just don't know how to call the items for this feed. I'm trying to make the comments parse with this post/feed but I get essentially nothing. I want the basic items like name of the post, caption, etc. I think if I just could get the name of the post I could figure it all out!
You are looping incorrectly
try this
foreach($fb_response->data as $item){
echo 'Message: ' . $item->message . '<br />';//there is no name returned on a comment
echo 'From ID: ' . $item->from->id . '<br />';
echo 'From Name: ' . $item->from->name . '<br />';
echo 'Message: ' . $item->message . '<br />';
echo 'Timestamp: ' . $item->created_time . '<br /><br />';
}
Do you have warnings/errors displayed? Ensure that you have extension=php_openssl.dll (or .so) enabled in your php.ini or you will get no results. This is because you are fetching from a secure site.
Also $item->name is an undefined property in the JSON. Perhaps you mean $item->id. Everything else looks ok.
Why aren't you using the PHP SDK?
https://developers.facebook.com/docs/reference/php/
Related
I'm working on a script that will retrieve API information of the 'near earth objects' from NASA's website. User selects date, api grabs the information and displays it. How do I fix the foreach in this script? would appreciate some help with this.
$jsonAsteroids = file_get_contents("https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-08-01&end_date=2018-08-04&api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo");
$data = json_decode($jsonAsteroids, true);
echo "<h4>Retrieving the first element (i.e. \"links\") of the JSON structure</h4>";
var_dump( $data["links"]);
echo "<h4>Retrieving the first element (i.e. \"next\") inside the \"links\" element</h4>";
echo( $data["links"]["next"]);
You were very close. Your main issue was that you used json_decode(..., true); which gives you an array, but then used the object->property syntax instead of object['property']. My suggestion is to use json_decode without the 2nd argument in this case.
Finally, your 2nd foreach was malformed.
<?php
$result = file_get_contents("https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-08-01&end_date=2018-08-04&api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo");
$data = json_decode($result);
foreach ($data->near_earth_objects as $date => $objects) {
echo "<p>" . count($objects) . " objects detected on $date</p>";
echo "<ol>";
foreach ($objects as $object) {
echo "<li>" . $object->name . " <a href='" . $object->nasa_jpl_url . "'>" . $object->nasa_jpl_url . "</a><br>";
echo "Diameter of the object: " . $object->estimated_diameter->meters->estimated_diameter_min . "-" . $object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($object->close_approach_data as $close_approach) {
echo "<li>Close approach: " . $close_approach->close_approach_date . " traveling at a velocity of " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul>";
}
echo "</ol>";
}
$refPointer = $site_to_be_extracted . $a->href;
echo $refPointer . '<br>';
generates in output (Chrome browser):
http://www.hackingwithphp.com/1/1/0/is-this-book-for-you
You would expect the following line to generate a correct <a> tag. That is a correct link
print '<a href=' . $refPointer . '>' . $a . '</a>';
Instead it generates in output:
localhost/1/1/0/is-this-book-for-you
So both the protocol (HTTP) and the $site_to_be_extracted (www.hackingwithphp.com) are lost. And replaced by "localhost"
Why? What could be the problem
This is the function where it's all happening. Now it works fine
include('simple_html_dom.php');
.
.
function createChapterContent ($site_to_be_extracted, $chapter_to_be_extracted) {
$html = file_get_html($chapter_to_be_extracted);
$refPointer;
foreach($html->find('ol') as $ol) {
foreach($ol->find('li') as $li) {
// echo '<br>' . $li->innertext ;
foreach($li->find('a') as $a) {
// Original. Causing the reported problem
// $refPointer = $site_to_be_extracted . $a->href;
// echo $refPointer . '<br>';
// changed to (see below). Now the output is correct
// Why?
$a->href = $site_to_be_extracted . $a->href;
$refPointer = $a->href;
print '<a href=' . $refPointer . '>' . $li->innertext . '</a>' . '<br>';
break;
};
}
}
I have emails and street addresses stored in a MySQL database. I want to use PHP to run a proximity/geospatial search and send an email to all the results within a given radius.
I can get the Email/Addresses in a foreach loop, but now I don't know how to send the email to all the results within the query result.
My code is like this:
foreach ($Ergebnis as $Eintrag) {
echo '<li><strong>' . htmlentities($Eintrag->Name) . '</strong><br />' . "\n";
echo htmlentities($Eintrag->Email) . '<br />' . "\n";
echo htmlentities($Eintrag->Strasse) . '<br />' . "\n";
echo htmlentities($Eintrag->PLZ) . ' ' . htmlentities($Eintrag->Ort) . '<br />' . "\n";
if (!empty($Eintrag->Website)) {
echo 'Website: ' . htmlentities($Eintrag->Website) . '<br />' . "\n";
}
Thank you very much!
I am trying to parse a simple response from the server and use its values.
I was able to get the required information as follows:
The actual response :
AccountID=0&Authenticated=1&ResponseCode=0&ResponseText=Success
What I need is separate values for :
AccountID
Authenticated
ResponseCode
ResponseText
My code so far :
$tempValue = explode("\n", $response);
foreach($tempValue as $row => $data)
{
//get row data
$row_data = explode('&', $data);
$row_internal = explode('=', $row_data);
$info2[$row]['id'] = $row_internal[0];
$info2[$row]['name'] = $row_internal[1];
$info2[$row]['description'] = $row_internal[2];
$info[$row]['id'] = $row_data[0];
$info[$row]['name'] = $row_data[1];
$info[$row]['description'] = $row_data[2];
echo 'Account ID: ' . $info[$row]['id'] . '<br />';
echo 'Authenticated: ' . $info[$row]['name'] . '<br />';
echo 'Response Code: ' . $info[$row]['description'] . '<br />';
echo '<br></br>';
echo 'Account ID: ' . $info2[$row]['id'] . '<br />';
echo 'Authenticated: ' . $info2[$row]['name'] . '<br />';
echo 'Response Code: ' . $info2[$row]['description'] . '<br />';
}
Result for the above code :
Account ID: AccountID=0
Authenticated: Authenticated=1
Response Code: ResponseCode=0
Account ID:
Authenticated:
Response Code:
What I needed was just the values for the fields like :
Account ID: 0
Authenticated: 1
Response Code: 0
If this is a query string response, then no need to explode, there is a better tool that handles this well.
Just use parse_str().
Simple one line response example:
$response = 'AccountID=0&Authenticated=1&ResponseCode=0&ResponseText=Success';
parse_str($response, $data);
echo '<pre>';
print_r($data);
Or if the response looks like a multiline string like this, you could apply it like:
$response = "AccountID=1&Authenticated=1&ResponseCode=0&ResponseText=Success
AccountID=2&Authenticated=1&ResponseCode=0&ResponseText=Success
AccountID=3&Authenticated=1&ResponseCode=0&ResponseText=Success
";
$responses = explode("\n", $response);
foreach ($responses as $key => $value) {
parse_str($value, $data);
if(!empty($data)) {
echo 'Account ID: '.$data['AccountID'] .'<br/>';
echo 'Authenticated: '.$data['Authenticated'] .'<br/>';
echo 'Response Code: '.$data['ResponseCode'] .'<br/>';
echo 'Response Text: '.$data['ResponseText'] .'<br/>';
echo '<br/>';
}
}
That's my first post here on SO.
I'm using PHP to get Facebook friends statuses. In particular, I've tried to retrieve all public statuses from one of my facebook friends, but it happens only the first 100 statuses. I need to get all the statuses and to write them in a text file. this is the code I'm using, patched up from many answers I read here on SO.
$i=0;
$result = $facebook->api('/my_friend_ID/statuses/',
array('access_token' => $facebook->access_token,'limit'=>100,));
//'offset'=>50,used before limit, it push the limits forward by 50, it doesn't go beyond it
//'since'=>2010, I read on SO there was even this field, but I can't make it work.
foreach($result['data'] as $post)
{
echo $i . '<br>';
echo $post['id'] . '<br>';
echo $post['from']['name'] . '<br>';
echo $post['from']['id'] . '<br>';
echo $post['name'] . '<br>';
echo $post['message'] . '<br>';
echo '*---------------------------------------------------*' . '<br>';
$i++;
$write_file = fopen("esempio.txt","a");
$message = $post['message'] . '<br>';
fwrite($write_file,$message);
fclose($write_file);
}
so, to be clearer, how to get all friends statuses (old and new) in a text file?
You need to use paging https://developers.facebook.com/docs/reference/api/pagination/
$the_statuses = array();
$your_statuses = $facebook->api("/my_friend_ID/statuses/");
while ($your_statuses['data'])
{
$the_statuses = array_merge( $the_statuses, $your_statuses['data'] );
$paging = $your_statuses['paging'];
$next = $paging['next'];
$query = parse_url($next, PHP_URL_QUERY);
parse_str($query, $par);
$your_statuses = $facebook->api(
"/my_friend_ID/statuses/", 'GET', array(
'limit' => $par['limit'],
'until' => $par['until'] ));
}
Then you can do the loop through all statuses
foreach($the_statuses['data'] as $post)
{
echo $i . '<br>';
echo $post['id'] . '<br>';
echo $post['from']['name'] . '<br>';
echo $post['from']['id'] . '<br>';
echo $post['name'] . '<br>';
echo $post['message'] . '<br>';
echo '*---------------------------------------------------*' . '<br>';
$i++;
$write_file = fopen("esempio.txt","a");
$message = $post['message'] . '<br>';
fwrite($write_file,$message);
fclose($write_file);
}