I want to use facebook php api to post a comment,
but I only know the page url, no post id,
how to know the post id base on url given?
$post_data=array(
'message'=>'post a comment test',
'link'=>'http://www.persunmall.com/p19000', // how to know post id
);
// how can i know the post id base on the link?
$facebook->api('/'.$post_id.'/comments','post',$post);
The post ID can be found in the array returned from the call to $facebook->api.
For example:
$facebook->api(<your API call>);
Then you can find the post ID in $result['id'] when making a call to 'feed', as demonstrated here
$result = $facebook->api('/121468571287906/feed/','post',$attachment);
Additionally, Facebook's API returns a response object, which will contain a post ID for the original call. Try adding a return value to your original call, and then examining that. For example, examine the details of $returnObject for this code:
$returnObject = $facebook->api('/'.$post_id.'/comments','post',$post);
Please refer to the Facebook API reference for details.
Related
I want to develop a PHP project for getting the content of an Instagram post (via the post URL). I mean, for example, the crawler getting this Url and returning :
#TBT Used to love carrying them both at once #twinmom #mycoconuts
#mylifeinmyhands #toobignow #istilltrysometimes #LOVE #forevermybabies
After I used the file_get_contents, the of returned response is empty. I don't know how to fix this problem. Can you help me?
I don't want to use the Instagram API!
You shouldn't use this since it will break as soon as Instagram makes a change to their markup, but here you go:
<?php
$instagram_url = "https://www.instagram.com/p/BX6IowXFbq9";
$source = file_get_contents($instagram_url);
preg_match('/<script type="text\/javascript">window\._sharedData =([^;]+);<\/script>/', $source, $matches);
$data = json_decode($matches[1]);
echo $data->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_to_caption->edges[0]->node->text;
This is little late but I am able to fetch Instagram timeline, Specific Media Details.
E.g:
Fetch user's list of post
If Instagram User page url is https://www.instagram.com/marvel then to fetch this page data you can use below URL
https://www.instagram.com/marvel?__a=1
Fetch specific media post details
If Instagram post detail page url is https://www.instagram.com/p/BfeD8RxHwnC/?taken-by=marvel then to fetch this page data you can use below URL
https://www.instagram.com/p/BfeD8RxHwnC/?taken-by=marvel&__a=1
Here's some code:
$facebookUrl = 'https://graph.facebook.com/'.$facebookPageId.'/posts?&access_token='.$facebookAppId.'|'.$facebookAppSecret;
$facebookData = json_decode(curlRequest($facebookUrl))->data;
curlRequest is successfully returning data but it's limited. The response has the following items:
message
story
created_time
id
It's bad enough these don't include a photo (which all of these posts do) but what's worse is that I have no link that takes me to the post.
Twitter has a redirect using 'https://twitter.com/statuses/'. $post->id; does Facebook have something similar? Or better yet how do I get all of the data for these posts?
Link to the post would be
$facebookUrl = 'https://graph.facebook.com/'.$facebookPageId.'/posts?&access_token='.$facebookAppId.'|'.$facebookAppSecret;
$facebookData = json_decode(curlRequest($facebookUrl))->data;
$link = "http:/fb.com/".$facebookData->id; //This short link will redirect to the pages' post
The $facebookData->id is made up of unique values i.e. the part before "_" (underscore) describes the parent (page,user,group,event) and string after underscore is the post id of its parent.
Adding to Nishant's helpful answer above, Facebook has a lot of other data you can get as part of the Post object, as outlined in their docs. By default, though, all it sends is
message
story
created_time
id
In order to retrieve any other fields, the request has to specify those fields in the query params. For example, if I want the picture and link associated with a Post, I would construct my GET request thus:
https://graph.facebook.com/POST_ID?fields=picture,link&access_token=YOUR_TOKEN
And I would get a response like this:
{
"link": "http://example.com/link",
"picture": "https://example.com/picture",
"id": POST_ID
}
I don't know PHP so can't give the specific syntax for OP's question, but this should get you most of the way there.
Example link for Facebook post:
facebook.com/123123123/posts/890890890/
pattern:
facebook.com/{{page_id}}/posts/{{post_id}}/
where from API post_id => {{page_id}}_{{post_id}}
I just found out that Facebook updated their graph api and now require an access token to retrieve like counts for a page to be displayed on an external website. I went through the motions of setting up an app to get the token and have the following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/myFacebookName?access_toke=MyAppId|MySecretToken'));
print_r($fbData);
It seems to only be returning objects:
stdClass Object ( [name] => My Facebook Name [id] => id number )
And that is all, giving me nothing to parse through. The name and id coming through are correct so there is a connection happening. Note that I have edited out some information so MyFacebookName and MyAppId|MySecretToken are actually populated with the correct info. Any ideas how to get the full JSON list to grab page likes? Am I missing something?
UPDATE
Thanks to Tobi and further reading the graph api, documentation I was able to get the number of likes with with following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/name?fields=likes&access_token=appId|accessToken'));
<?php echo number_format($fbData->likes);?>
You need to specifically request the fields you want in the response. In your case this would be
https://graph.facebook.com/myFacebookName?fields=id,name,likes&access_toke=MyAppId|MySecretToken
I don't understand why you say there "nothing to parse", because the object IS returned and can be used. So what's the problem?
Using echo $fbData['likes'] should give you the number of likes.
See
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#fields
https://developers.facebook.com/docs/graph-api/reference/page#Reading
I used the following code to get title and like count of youtube video:
$gdata_url = 'http://gdata.youtube.com/feeds/api/videos/'.$videoId.'?v=2&alt=jsonc';
$json_content = file_get_contents($gdata_url, 0, null, null);
But currently title of json response always contains https://youtube.com/devicesupport instead of actual title and json response does not contain viewCount.
Could you please advise how to solve the issue?
Youtube API v2 has been deprecated. You can find how to get video info with v3 here.
You use the part parameter to define what properties you want to retrieve. Snippet gives you most of the properties including title and statistics returns the like count. You can use "snippet,statistics" to get both with one call.
I need a way to display videos from a specific channel on a page using PHP.
I have authenticated my app and I can use some methods using the advanced API. I am using the official vimeo PHP library to connect.
Below is what I am trying to do and when I dump the array I do not get anything. I can get info back from using get videos from the entire account method.
require_once('/url/vimeo/vimeo.php');
$vimeo = new phpVimeo('number', 'number');
$vimeo->setToken('number','numbers');
$videos = $vimeo->call('vimeo.channels.getVideos', array('ACCOUNT' => 'NAME'));
If I put the channel name where ACCOUNT is I will get an invalid signature error.
Is it worth using something like simple HTML parser for PHP and doing it that or worth sticking with the advanced API?
I would highly advise using the advanced api. If you parse the html, it will break any time vimeo changes their channel pages. Additionally, channels have more than one layout
eg: vimeohq and nicetype
The second parameter of the "call" function should be any querystring parameters the api method requires.
In the case of "vimeo.channels.getVideos" you can provide
channel_id
user_id
page
per_page
summary_response
full_response.
To experiment with the getVideos method, you can use the playground.
So in the end, I believe you want the function to look like this..
$videos = $vimeo->call('vimeo.channels.getVideos', array('channel_id' => 'NAME'));
where NAME is either the channel id, or the channel name (the channel name matches the url slug, so for example "nicetype" not "nice type"