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.
Related
I am trying to output specific information coming from JIRA, I have JSON looping and results showing, but the results furthest down the nest are only showing the first character.
I have tried following the suggestions here but this seems to be for results at the top level and I can't seem to get it working with my code.
<?php
$username = '*************';
$password = '*************';
$url = '<JIRA-DOMAIN>/rest/api/2/search?jql=project=ICI&fields=key,summary,description&maxResults=10';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$issue_list = (curl_exec($curl));
$json = json_decode($issue_list, TRUE);
?>
<div class="toto">
<?php
foreach($json['issues'] as $obj){ ?>
<h2><?php echo $obj['id']; ?></h2>
<?php foreach($obj['fields'] as $parc){ ?>
<a href="<?php echo $parc['summary']; ?>">
<span><?php echo $parc['description']; ?></span>
</a>
<?php }
} ?>
</div>
When I echo $issue_list I get the following:
Output Shortened
{
"expand":"schema,names",
"startAt":0,
"maxResults":10,
"total":9,
"issues":[
{
"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id":"10058",
"self":"<domain>/rest/api/2/issue/10058",
"key":"ICI-9",
"fields":{
"summary":"ToS and PP button removal",
"description":"The Terms of Service and Privacy Policy buttons go to a page on the website so they can't use the app after. They also go to a page of nothing because they aren't written so they need to be deleted"
}
},
{
"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id":"10048",
"self":"<domain>/rest/api/2/issue/10048",
"key":"ICI-1",
"fields":{
"summary":"Create Launch Image",
"description":"Create launch image for app. Resolution should be: 2048px × 1536px\r\n\r\n[~james.whitaker] I got this info from [here|https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/]"
}
}
]
}
But the loop output only shows the first character for the Description and Summary as seen below but shows the entire ID.
Any help would be highly appreciated.
Your problem is in this foreach loop:
<?php foreach($obj['fields'] as $parc){ ?>
<a href="<?php echo $parc['summary']; ?>">
<span><?php echo $parc['description']; ?></span>
</a>
<?php }
$parc will have the value of $obj['fields']['summary'] and then $obj['fields']['description'] in the loop, so when you try and index it with ['summary'] you are using an invalid index which is translating to 0 (hence the single character). Try this instead (remove the foreach):
<a href="<?php echo $obj['fields']['summary']; ?>">
<span><?php echo $obj['fields']['description']; ?></span>
</a>
i think fields isn't array.
Change $parc['summary'] to $obj['fields']['summary']
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
I am going to pull my hair out. Can anyone please help me get this to work I am sure it's something stupid.. I have got all the PHP errors to go away, but I can not get images to show up. Code below...
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="jquery.fancybox-1.3.4.css" type="text/css">
<script type='text/javascript' src='jquery.min.js'></script>
<script type='text/javascript' src='jquery.fancybox-1.3.4.pack.js'></script>
<script type="text/javascript">
$(function() {
$("a.group").fancybox({
'nextEffect' : 'fade',
'prevEffect' : 'fade',
'overlayOpacity' : 0.8,
'overlayColor' : '#000000',
'arrows' : false,
});
});
</script>
<?php
// Supply a user id and an access token
$userid = "1d458ab0c149424c812e664c32b48149";
$accessToken = "c195717e379f48c68df451cc3d60524a";
// 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 } ?>
<?php endif ?>
</html>
What you need is to add some checks at various points to find out what is coming back from Instagram and handle any issues. While debugging, sticking var_dump() all over the place can be helpful to see where issues lie.
Here is an example of your PHP section with some additional checks:
<?php
// Supply a user id and an access token
$userid = "USER ID";
$accessToken = "ACCESS TOKEN";
// 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);
$info = curl_getinfo($ch);
// Check a response was returned
if ($info['http_code'] == '404') {
echo ('Error: HTTP 404 returned, bad request');
die();
}
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/?access_token={$accessToken}");
$result = json_decode($result);
// Check the json_decode succeeded
if (empty($result)) {
echo "Error: JSON not returned from API";
die();
}
// Check no error was returned from Instagram
if ($result->meta->code != 200) {
echo "Error: ".$result->meta->error_message;
die();
}
?>
If you plan on doing a lot of work with the Instagram API, you may want to look at a library to do most of the heavy lifting. This one appears to be the most popular at present.
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
I use the following php code to get a user twitter followers. I want to export this data to a csv file and add a filter to save only followers with more than 100 followers.
<script src="http://code.jquery.com/jquery-latest.js"></script>
<?php
$trends_url = "http://api.twitter.com/1/statuses/followers/pthiongo.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
foreach($response as $friends){
$thumb = $friends['profile_image_url'];
$url = $friends['screen_name'];
$name = $friends['name'];
echo $friends['screen_name'];
?>
<a title="<?php echo $name;?>" href="http://www.twitter.com/<?php echo $url;?>"><img class="photo-img" src="<? php echo $thumb?>" border="0" alt="" width="40" /></a>
<?php
}
?>
First this , http://blog.gabrieleromanato.com/2012/06/jquery-get-twitter-followers-count/
Then use an if (followers_count > 99) in PHP to display
Programatically, you can request a list of up to 100 of your own followers with the Instagram API using the sample request below.
The sample request below is from SnippetLib:
https:api.instagram.comv1users3followed-by?access_token=ACCESS-TOKEN
This route (from nbyim.com) uses pagination to get around the rate limits:
from instagram.client import InstagramAPI
user_id=''
access_token = ''
client_secret = ''
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
followers = []
# Get the followers list
for p in api.user_followed_by(user_id=user_id, as_generator=True, max_pages=None):
followers.extend(p[0])
# Convert from an instagram.models.User list to a list of strings
followers = [str(u).replace('User: ', '') for u in followers]
print len(followers), 'followers'
print followers
To automate the CSV process, you can use a service like Crowdbabble: https://www.crowdbabble.com/download-all-instagram-followers/