Getting an image from a facebook post on a public page - php

I'm trying to show a facebook feed on my website, which is working. But I only managed to show the text of the post, not the image attached to it (or if possible, if there are multiple images, only the first one or biggest one).
I tried looking here for the correct name to get it using the API
This is my code now (which shows facebook posts in an owl carousel):
function fetchUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
// You may need to add the line below
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
//App Info, needed for Auth
$app_id = "1230330267012270";
$app_secret = "secret";
//Retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=1230330267012270&client_secret=secret");
$json_object = fetchUrl("https://graph.facebook.com/267007566742236/feed?{$authToken}");
$feedarray = json_decode($json_object);
foreach ( $feedarray->data as $feed_data )
{
if($feed_data->message != ''){
$facebookfeed .= '
<div class="item">
<div class="product-item">
<img class="img-responsive" src="images/siteimages/imgfacebook.jpg" alt="">
<h4 class="product-title">'.$feed_data->name.'</h4>
<p class="product-desc">'.$feed_data->message.'</p>
<p>'.$feed_data->story.'</p>
<img src="'.$feed_data->picture.'">
<p>Lees meer</p>
</div><!-- Product item end -->
</div><!-- Item 1 end -->';
}
}
echo $facebookfeed;
Looking at the Facebook documentation I thought $feed_data->picture would work, but it returns nothing.

To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data.
Source: https://developers.facebook.com/docs/apps/changelog#v2_4

Related

PHP Fetch image from DM Twitter using REST API?

I have problem when I want to fetch image that was sent to my DM.
So when I fetch the DM data from API, I got the media id for the image but how to fetch it?
I want to fetch the image and tweet it to my account.
So basically:
Someone send DM to me with media
I read my account DM from API
Tweet DM content to my account
Here is my code
$AMConnection = new \Twitter\OAuth(AM_CONSUMER_KEY, AM_CONSUMER_SECRET);
$AMConnection->setAccessToken($amService->token, $amService->secret);
$jsonData = json_decode(file_get_contents("./fetchDM.json"));
foreach ($jsonData as $json) {
if (!isset($json->entities->media)) continue;
$medias = $json->entities->media;
$postParam['status'] = $json->text;
foreach ($medias as $image) {
$postParam['status'] = str_replace($image->url, "", $postParam['status']);
$postParam['media_ids'][] = $image->id_str;
}
echo "SEND TWEET".PHP_EOL;
$return = $AMConnection->post('statuses/update', $postParam);
var_dump($return);
if (isset($return->errors) and count($return->errors) > 0) {
foreach ($return->errors as $error) {
if ($error->code == 89) {
$amService->token = null;
$amService->secret = null;
$amService->save();
break;
}
}
}
}
On my code, I was tried to tweet media using media_id but it doesn't work.
Sorry for bad english
Finally it's solved.
So I've been look for this one on some forums but only 2 forum has answer, 1 on twitter forum (sorry I don't have link) and another 1 StackOverflow
I'm using library and try messing out with it but deadend. The library that I've used to request REST API, use OAuth request from URL and POST data. Yeah, it's totally work (only for REST API) but this one is totally different.
To fetch image from DM, YOU MUST REQUEST USING HEADER AUTHORIZATION and no other way.
On my sample, I want to try to fetch https://ton.twitter.com/1.1/ton/data/dm/863758539021365258/863758532109246464/VSSysEvy.jpg (This one is image url from DM, so only me and someone who've sent me that image only can see it).
I'm using CURL PHP to fetch the image so the request will be like this
<?php
$this->http_info = array();
$ci = curl_init();
$url = 'https://ton.twitter.com/1.1/ton/data/dm/863758539021365258/863758532109246464/VSSysEvy.jpg';
$headerAuth = 'Authorization: OAuth oauth_version="1.0",
oauth_nonce="{autoGeneratedNonce}",
oauth_timestamp="{time()}",
oauth_consumer_key="{yourAppToken}",
oauth_token="{userToken}",
oauth_signature_method="HMAC-SHA1",
oauth_signature="{autoGeneratedSignature}"';
/* Curl settings */
curl_setopt($ci, CURLOPT_USERAGENT, 'LShaf User Agent');
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_HTTPHEADER, [$headerAuth]);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_HEADER, FALSE);
curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);
curl_close ($ci);
Note: You MUST use method GET and put Authorization on HEADER.
And for the last thing, I'm sorry for my bad english.

It is possible get the media of another user? Instgram API

This code that I took form another web, works! but only when I put my Instragram ID. Otherwise when I want to get a media from another user (changing the variable userid) it stops working.
To be sure I use https://www.otzberg.net/iguserid/index.php to get the user ID.
And to get the Access Token (only with my account): http://instagram.pixelunion.net/
<?php
// Supply a user id and an access token
$userid = "-----";
$accessToken = "---";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php if(!empty($result->data)): ?>
<?php foreach ($result->data as $post): ?>
<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
<?php endif ?>
The link you posted clearly indicates that the token only works with your own images.
In order to display your Instagram photos on your own website,
you are required to provide an Instagram Access Token.
You should try a different API or method to show images from another user.

Php curl missing content from page

$url = "http://www.ksl.com/index.php?nid=231&sid=74268&cat=&search=&zip=&distance=&min_price=&max_price=&type=&category=&subcat=&sold=&city=&addisplay=%5BNOW-1HOURS+TO+NOW%5D&sort=1&userid=&markettype=sale&adsstate=&nocache=1&o_facetSelected=true&o_facetKey=ad+posted&o_facetVal=Last+Minute&viewSelect=list&viewNumResults=48&sort=1";
$html = file_get_contents($url);
This returns some of the page content. I think that because the page I am trying to curl uses jquery to insert the listings the curl executes before the jquery populates the page?
Any ideas on how to get the full contents of the search curl?
Try this:
$url = 'http://www.ksl.com/index.php?nid=231&sid=74268&cat=&search=&zip=&distance=&min_price=&max_price=&type=&category=&subcat=&sold=&city=&addisplay=%5BNOW-1HOURS+TO+NOW%5D&sort=1&userid=&markettype=sale&adsstate=&nocache=1&o_facetSelected=true&o_facetKey=ad+posted&o_facetVal=Last+Minute&viewSelect=list&viewNumResults=48&sort=1';
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data($url);
echo $returned_content;
No they are not getting the listings via jQuery, AJAX. There are no XHR requests.
A view source shows the listings.
I just used this code to get
header('Content-Type: text/plain; charset=utf-8');
$url = 'http://www.ksl.com/index.php?nid=231&sid=74268&cat=&search=&zip=&distance=&min_price=&max_price=&type=&category=&subcat=&sold=&city=&addisplay=%5BNOW-1HOURS+TO+NOW%5D&sort=1&userid=&markettype=sale&adsstate=&nocache=1&o_facetSelected=true&o_facetKey=ad+posted&o_facetVal=Last+Minute&viewSelect=list&viewNumResults=48&sort=1';
$data = file_get_contents($url);
echo $data;
The Result
Partial edited
Black Xbox 360 320gb Hard Drive Plus controllers and G...</a>
<span style="color: #555;">Magna, UT
| 1 Min
<div class="adDesc">
Selling because I got it used then only used it once, works fantastically and comes with lots of games plus 4 controllers and a keyboard for the contr
<a class="listlink" href="?nid=218&ad=34170943&cat=&lpid=&search=&ad_cid=7">more...</a>
<div class="priceBox">
<a href="?nid=218&ad=34170943&cat=&lpid=&search=&ad_cid=7">
<span >$450<span class="priceCents">00
<!-- <div class="adDays">1 Min -->
<!--<div class="adTime">Dec 31, 1969-->
<div class="detailBox">
Great PS4 Bundle</a>
<span style="color: #555;">Spanish Fork, UT
| 1 Min
Have here a great PS4 bundle with 6Games 1controller and 3months of PS Network. Still has the box. Text 801-633-1659
<span >$400<span class="priceCents">00

Limit Instagram Feed in Php-Fetch

i am using the code below, to show some photos of my instagram-account on my website. it just fetches all the images of my account in the div. Is there a way to limit the fetched data to 10 Images or so ?
Cant figure out how to do that..
thanks for your help!
<div id="instagramfeed">
<?php
// Supply a user id and an access token
$userid = "123xy";
$accessToken = "123xy ";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php foreach ($result->data as $post): ?>
<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?> <br><br><br><br>
</div>
You could use the count parameter.
https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}&count=10
It was a problem in Instagram Developer Console. max_id and min_id doesn't work there.
For anyone interested - i found a solution for this problem:
it doesnt work with: {$accessToken}&count=10
But it works with:
?access_token=123456789101112131415123123111&count=10

Trying to create Lightbox gallery for only one foreach element

Okay, so here's what I'm trying to achieve!
I have a PHP script that I pull information into a foreach() statement from an API/JSON setup.
This is some of my code:
$ch = curl_init();
$category=$_GET['category'];
$url="http://www.someurl.com/api/listings?token=f716909f5d644fe3702be5c7895aa34e&group_id=10046&species=".$category;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: f716909f5d644fe3702be5c7895aa34e',
));
$json = json_decode(curl_exec($ch), true);
// Functions relating to the Echo Code
foreach($json['listings'] as $listing)
{
$short_personality=substr($listing['personality'],0,500);
$peturl="http://www.someurl.com/?variable=".$listing['id'];
$medium_photo=$listing['photos'][0]['large_340'];
$gender_class=strtolower($listing['gender']);
$breed_class=strtolower($listing['species']);
$name=($listing['name']);
Then, I'm creating the image to display with the following code:
// Echo the output
echo'<div class="animal">
<div class="animal-image">';
if ($listing['photos'] > 1) {
echo '<a class="size-thumbnail thickbox" rel="gallery" href="'.$medium_photo.'">
<img src="'.$medium_photo.'" class="image-with-border" alt="">
<div class="border" style="width: 340px; height: 340px;">
<div class="open"></div>
</div>
</a>';
}
else {
echo '<a class="size-thumbnail thickbox" href="'.$medium_photo.'">
<img src="'.$medium_photo.'" class="image-with-border" alt="">
<div class="border" style="width: 340px; height: 340px;">
<div class="open"></div>
</div>
</a>';
}
So, for example, the API pulls in 25 items that are all formatted into their individual section, each section has it's own initial photo. When I click on that photo, the lightbox loads.
BUT, it loads the lightbox for ALL images in the API call, so I'm getting 144 images available in the lightbox.
Is there a way to limit the photos in the lightbox for each item that's being retrieved?
Any help would be appreciated.
this answer is based on my assumption that you use thickbox plugin (if thats the case please update your question details :D )
if you want every image to be "alone" and not in a gallery then every link must have a different rel="name-of-individual-image-gallery-whatever"
so you can do something like
// Functions relating to the Echo Code
// add a counter
$inc=0;
foreach($json['listings'] as $listing)
{
$short_personality=substr($listing['personality'],0,500);
$peturl="http://www.someurl.com/?variable=".$listing['id'];
$medium_photo=$listing['photos'][0]['large_340'];
$gender_class=strtolower($listing['gender']);
$breed_class=strtolower($listing['species']);
$name=($listing['name']);
// add this
$unique_gallery_name="individual-gallery-".$inc;
$inc++;
// the rest of your code...
// .
// .
// .
and then add that rel attribute to your output code
<a class="size-thumbnail thickbox" rel="'.$unique_gallery_name.'"
so each link will have one rel like this rel="individual-gallery-0", the next link rel="individual-gallery-1", the next link rel="individual-gallery-2" ... and so on. The result will be every image to be in a "unique" gallery, "alone"

Categories