get comments of post on facebook in php via graph - php

i want to get facebook comments on public post via graph.facebook
i want to get the name, the content and the id of the commenter
example of the graph
http://graph.facebook.com/comments/?ids=391265991032089
mycode so far
<?php
$data = file_get_contents('http://graph.facebook.com/comments/?ids=391265991032089');
$json = $data;
$obj = json_decode($json);
$comm_no = $obj->391265991032089->{'data'};
echo("<pre>");
var_dump($comm_no);
?>
wich gives me
array(25) {
[0]=>
object(stdClass)#437 (7) {
["id"]=>
string(31) "337379989797698_337380613130969"
["from"]=>
object(stdClass)#433 (2) {
["id"]=>
string(15) "100002978598053"
["name"]=>
string(28) "Àñä Båñòtá Bãskõtã"
}
["message"]=>
string(38) "سقفه لاخوكو ابو جبل :D"
["can_remove"]=>
bool(false)
["created_time"]=>
string(24) "2014-12-15T18:58:24+0000"
["like_count"]=>
int(10)
["user_likes"]=>
bool(false)
}
how to stip it down to just show only
name , message and id
isearched alot and didnt find any thing

If there is more than an answer then use the following code:
<?php
$data = file_get_contents('http://graph.facebook.com/comments/?ids=391265991032089');
$json = $data;
$obj = json_decode($json);
$comm_no = $obj->391265991032089->{'data'};
foreach($comm_no as $answer_id => $v) {
echo $v->from->id . '<br />';
echo $v->from->name. '<br />';
echo $v->message. '<br />';
}
?>

Related

How to read a json file into html table php?

Okay so this is my first time posting on this website. I need a little help, I am trying to add data from a json API into a html table. This is what I have so far.
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
var_dump(json_decode($json));
<?
Here is my Output:
object(stdClass)#1 (2) { ["status"]=> string(3) "200" ["response"]=> object(stdClass)#2 (5) { ["onlineId"]=> string(6) "x0--II" ["avatar"]=> string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png" ["plus"]=> int(0) ["aboutMe"]=> string(0) "" ["trophies"]=> object(stdClass)#3 (6) { ["trophyLevel"]=> int(3) ["progress"]=> int(7) ["platinum"]=> int(0) ["gold"]=> int(2) ["silver"]=> int(6) ["bronze"]=> int(19) } } }
I want to display the image as well as all the values. Thanks for any help!
You would put the json_decode data into a PHP variable, and access it like a normal PHP object array
Here's a quick example:
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
$content = json_decode($json);
echo "ID: ".$content->response->onlineId."<br />";
echo '<img src="'.$content->response->avatar.'" />';
echo "<table border=1>";
foreach ($content->response->trophies as $key => $value){
echo "<tr>
<td>".$key."</td>
<td>".$value."</td>
</tr>";
}
echo "</table>";
?>
That would loop through your trophies put it into an HTML table.
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
#var_dump(json_decode($json, true));
/* json formatted for readability
object(stdClass)#1 (2) {
["status"]=> string(3) "200"
["response"]=> object(stdClass)#2 (5) { // response is an array()
["onlineId"]=> string(6) "x0--II"
["avatar"]=> string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png"
["plus"]=> int(0)
["aboutMe"]=> string(0) ""
["trophies"]=> object(stdClass)#3 (6) { // trophies is an array()
["trophyLevel"]=> int(3)
["progress"]=> int(7)
["platinum"]=> int(0)
["gold"]=> int(2)
["silver"]=> int(6)
["bronze"]=> int(19)
}
}
}
*/
$json = json_decode($json, true);
$rsp = $json['response']; // response array
$trophies = $rsp['trophies']; // trophy array
// examples
$avatar = $rsp['avatar'];
$onlineId = $rsp['onlineId'];
$trophylevel = $trophies['trophyLevel'];
// inline html example
echo("<div><img src=\"".$rsp['avatar']."\" /></div>");
// or
echo("<div><img src=\"".$json['response']['avatar']."\" /></div>");
echo("<div>".$json['trophies']['progress']."</div>");
// or
echo("<div>".$json['response']['trophies']['progress']."</div>");
?>

Print single field from JSON in PHP

I've been trying for hours now to get the title field from the json code. Below is my php code
$search = $_GET['search'];
$new = str_replace(' ', '+', $search);
$url = "http://api.themoviedb.org/3/search/movie?api_key=###&query=".$new;
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$title = $json_data->title;
echo $title;
this is the var dump of the json
array(4) { ["page"]=> int(1) ["results"]=> array(1) { [0]=> array(10) { ["adult"]=> bool(false) ["backdrop_path"]=> string(32) "/4uJZvmEMX6Z6ag3bzym5exLY9wI.jpg" ["id"]=> int(65) ["original_title"]=> string(6) "8 Mile" ["release_date"]=> string(10) "2002-11-08" ["poster_path"]=> string(32) "/dXzTrKwpbLpCqn8O70FUUhNbYQT.jpg" ["popularity"]=> float(3.792332418578) ["title"]=> string(6) "8 Mile" ["vote_average"]=> float(6.2) ["vote_count"]=> int(185) } } ["total_pages"]=> int(1) ["total_results"]=> int(1) }
the error i keep getting is Notice: Trying to get property of non-object
any help would be greatly appreciated.
$json_data = json_decode($json, true);
will return an array not object
so you need to use as
$json_data["title"];
NOTE : Your json decoded array is nested so you may need to use as in your case.
$json_data["results"][0]["title"];
Or better loop through and get the desired data.
I just started using PHP and I THINK this might help you... but i'n not sure
foreach($json_data as $key => $value){
if($key == "results"){
$json_data2 = $value;
}
}
foreach($json_data2 as $key => $value){
if($key == "original_title"){
$title = $value;
}
}
echo $title;

how to get only specific variable value from json response(facebook response)

I want only a specific value(say "name") from my generated output. I used this code
var_dump($request_object_details); to generate the below output.
What output I am getting:-
array(6) { ["id"]=> string(26) "514484461930096_1446926141" ["application"]=> array(2) { ["name"]=> string(18) "Pocket Financetest" ["id"]=> string(15) "270339389736782" } ["to"]=> array(2) { ["name"]=> string(12) "Prince Singh" ["id"]=> string(10) "1446926141" } ["from"]=> array(2) { ["name"]=> string(14) "Ashutosh Singh" ["id"]=> string(15) "100003645579131" } ["message"]=> string(16) "My Great Request" ["created_time"]=> string(24) "2013-01-17T03:45:36+0000" }
I used the below code to get the above output:
<?php
require_once('phps/fbsdk/src/facebook.php');
$config = array
(
'appId' => '270339389736782',
'secret' => '667e1795cc1f308f312d49d6b1c17cb8',
);
$facebook = new Facebook($config);
//get the request ids from the query parameter
$request_ids = explode(',', $_REQUEST['request_ids']);
//build the full_request_id from request_id and user_id
function build_full_request_id($request_id, $user_id) {
return $request_id . '_' . $user_id;
}
//for each request_id, build the full_request_id and delete request
foreach ($request_ids as $request_id)
{
$uid = $facebook->getUser();
$full_request_id = build_full_request_id($request_id, $uid);
$request_object_details = $facebook->api("/$full_request_id");
var_dump($request_object_details);//This is giving me the output
try {
$delete_success = $facebook->api("/$full_request_id",'DELETE');
if ($delete_success) {
echo "Successfully deleted " . $full_request_id;}
else {
echo "Delete failed".$full_request_id;}
}
catch (FacebookApiException $e) {
echo "error=".$e;}
}
?>
Your question is not clear but still I am posting some sort of code for you.
Use json_decode
Do like this : It will return object
$obj = json_decode($request_object_details);
$result= $obj->id;
OR
You can use json_decode and convert it into the array:
$request_object_details = $facebook->api("/$full_request_id");
$result=json_decode($request_object_details, true);
The second parameter will make decoded json string into an associative arrays.
Now you can directly use :
echo $result['id'];
echo $result['application'];
and so on...
Hope it'll help you.

Containers listing in Windows Azure SDK for PHP

I am trying to list the containers and so far having no luck at all... i already tried
$aBlobContainer = $blobRestProxy->listContainers();
for($i = 0;$i<= count($aBlobContainer); $i++)
{
echo 'Blob Container name is: '.$aBlobContainer[$i]->Name."\n";
}
but i am having error
Cannot use object of type WindowsAzure\Blob\Models\ListContainersResult as array
Been trying to work around it all day just can't seem to make any progress... let me know if i am doing something silly or if there is a better way to find out if the container already exist? Thanks!
EDIT:
var_dump of the variable $aBlobContainer came up as
object(WindowsAzure\Blob\Models\ListContainersResult)#42 (5) {
["_containers":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
array(2) {
[0]=>
object(WindowsAzure\Blob\Models\Container)#48 (4) {
["_name":"WindowsAzure\Blob\Models\Container":private]=>
string(6) "abc123"
["_url":"WindowsAzure\Blob\Models\Container":private]=>
string(48) "http://orig.blob.core.windows.net/abc123"
["_metadata":"WindowsAzure\Blob\Models\Container":private]=>
array(0) {
}
["_properties":"WindowsAzure\Blob\Models\Container":private]=>
object(WindowsAzure\Blob\Models\ContainerProperties)#47 (2) {
["_lastModified":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
object(DateTime)#49 (3) {
["date"]=>
string(19) "2012-11-29 01:32:20"
["timezone_type"]=>
int(2)
["timezone"]=>
string(3) "GMT"
}
["_etag":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
string(19) ""0x8CF9BE88256926F""
}
}
[1]=>
object(WindowsAzure\Blob\Models\Container)#46 (4) {
["_name":"WindowsAzure\Blob\Models\Container":private]=>
string(8) "multi123"
["_url":"WindowsAzure\Blob\Models\Container":private]=>
string(50) "http://orig.blob.core.windows.net/multi123"
["_metadata":"WindowsAzure\Blob\Models\Container":private]=>
array(0) {
}
["_properties":"WindowsAzure\Blob\Models\Container":private]=>
object(WindowsAzure\Blob\Models\ContainerProperties)#45 (2) {
["_lastModified":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
object(DateTime)#53 (3) {
["date"]=>
string(19) "2012-11-29 03:13:16"
["timezone_type"]=>
int(2)
["timezone"]=>
string(3) "GMT"
}
["_etag":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
string(19) ""0x8CF9BF69C25759F""
}
}
}
["_prefix":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
NULL
["_marker":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
NULL
["_nextMarker":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
NULL
["_maxResults":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
NULL
}
Looking at the Source Code:
$blobContainers = $blobRestProxy->listContainers(); //returns ListContainersResult
in order to get the listing of containers you'd have to do a subsequent call of:
$blobContainerArray = $blobContainers->getContainers(); //exposes the array of containers
Then you should be able to use that array in either a foreach or for statement. This workflow matches that of returning a list of blobs from within a container as seen in the README.md file:
try {
// List blobs.
$blob_list = $blobRestProxy->listBlobs("mycontainer");
$blobs = $blob_list->getBlobs();
foreach($blobs as $blob)
{
echo $blob->getName().": ".$blob->getUrl()."<br />";
}
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
$options = new ListContainersOptions();
$options->setPrefix("prefixxxx");
$blobContainers = $blobRestProxy->listContainers($options);
$blobContainerArray = $blobContainers->getContainers();
foreach ($blobContainerArray as $container)
{
Trace("Container: " . $container->getName());
}
From the error message, it looks like $blobRestProxy->listContainers() is returning an object. Try the code below.
$aBlobContainer = $blobRestProxy->listContainers();
foreach($aBlobContainer as $row) {
echo 'Blob Container name is: '.$row->Name."\n";
}
When accessing $aBlobContainer as an array (i.e. $aBlobContainer[$i]), it was probably giving the error.
* Edit *
foreach($aBlobContainer as $key => $row) {
echo $row->Name . "\n";
}

foreach loop in php get invalid argument supplied

I get "Invalid argument supplied" when using the following code. I can successfully parse an ip address and port number but i dont know how to get more than one at a time. My foreach loop is not working. Any ideas?
$dom = new DOMDocument();
$dom->loadHTMLFile($url);
$xml = simplexml_import_dom($dom);
$dom_results = $xml->xpath("/html/body/div[#id='subpagebgtabs']/div[#id='container']/table[#id='listtable']");
$ip_address = $dom_results[0]->tr->td[1]->span;
$ip_post = $dom_results[0]->tr->td[2];
$address_parts = $ip_address.":".$ip_post;
foreach ($address_parts as $address_full){
echo $address_full . "<br>";
}
$Dom_Results Output
["tr"]=>
array(50) {
[0]=>
object(SimpleXMLElement)#5 (3) {
["#attributes"]=>
array(2) {
["class"]=>
string(0) ""
["rel"]=>
string(7) "9054676"
}
["comment"]=>
object(SimpleXMLElement)#56 (0) {
}
["td"]=>
array(8) {
[0]=>
object(SimpleXMLElement)#57 (2) {
["#attributes"]=>
array(2) {
["class"]=>
string(20) "leftborder timestamp"
["rel"]=>
string(10) "1309047901"
}
["span"]=>
string(10) "2 minutes"
}
[1]=>
object(SimpleXMLElement)#58 (1) {
["span"]=>
string(13) "122.72.10.201"
}
[2]=>
string(3) "80"
I think this is what you're looking for:
// If results are found
if ( ! empty($dom_results) )
// Loop through each result. Based on your XPath query, the $dom_results
// contains tables. This loops through the rows of the first table.
foreach ( $dom_results[0]->tr as $row )
{
$ip_address = $row->td[1]->span;
$ip_post = $row->td[2];
// Output the address
echo $ip_address . ":" . $ip_post . "<br />";
}
it seems you want to extract all the ip address and port numbers and concatenate it like
ipaddress:port
So try this
foreach($dom_results as $dom) {
$ip = $dom->tr->td[1]->span;
$port = $dom->tr->td[2];
$address = $ip . ":". $port;
echo $address . "<br />";
}

Categories