Pulling instagram photos in php - php

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.

Related

Get Facebook Profile URL From Facebook Profile id Using PHP

I have a list of Facebook links in xls sheet with Facebook id something like this
and when i go through these links means if i access Facebook with id www.facebook.com/7500
i get urls something like this https://www.facebook.com/davesachs/
so my question is i want to do this with PHP, i have a PHP page which read data from xls sheet
my code here:
require_once 'src/SimpleXLSX.php';
if ( $xlsx = SimpleXLSX::parse('fburl.xlsx') ) {
$rows= $xlsx->rows();
foreach($rows as $data){
echo $data[0].'<br>';
}
} else {
echo SimpleXLSX::parseError();
}
its returning all Facebook link with id same that i am passing like www.facebook.com/7500 but i want it return URL / link of profile as https://www.facebook.com/davesachs/ ,if it is possible please help me to do that.
You can do something like that, Taken reference from here
<?php
require_once 'src/SimpleXLSX.php';
if ( $xlsx = SimpleXLSX::parse('fburl.xlsx') ) {
$rows= $xlsx->rows();
foreach($rows as $data){
getRedirectedUrl($data[0]);
}
} else {
echo SimpleXLSX::parseError();
}
function getRedirectedUrl($link){
$url=$link;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
// Uncomment to see all headers
/*
echo "<pre>";
print_r($a);echo"<br>";
echo "</pre>";
*/
echo $url; // Voila
}
?>

How to print an array element from JSON into PHP

If you access the webpage https://api.mercadolibre.com/items/MLB752465575 you will receive a JSON response. All I need to start is print the item "id" on the screen.
This is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$json_str = "https://api.mercadolibre.com/items/MLB752465575";
$obj = json_decode($json_str);
echo "id: $obj->id<br>";
?>
</body>
</html>
All I want is receive the MLB752465575 part into my browser.
How can I do it?
$json_str = "https://api.mercadolibre.com/items/MLB752465575";
The above does not retrieve the data it's saving the url to the var and that's not what you want.
You just need to fetch the content You can use cURL or file_get_contents()
cURL version:
<?php
$url = "https://api.mercadolibre.com/items/MLB752465575";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$r = curl_exec($curl);
curl_close($curl);
$array = json_decode($r, true);
echo "<pre>";
print_r($array);
echo "</pre>";
?>
file_get_contents version:
<?php
$r = file_get_contents('https://api.mercadolibre.com/items/MLB752465575');
echo "<pre>";
echo print_r(json_decode($r, true));
echo "</pre>";
?>
Both of them will work unless the remote website requires you to be human (has extra verifications to stop robot requests). cURL would be a better way if that were the case because you can fake a user agent using a header array.
Once you have the array build it's just a matter of accessing the required data. using $r as an array result of the remote json structure.
Use curl to get the result, and json_decode to turn it into an array.
<?php
$url = "https://api.mercadolibre.com/items/MLB752465575";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode != 200) {
echo "error " . $httpcode;
curl_close($ch);
return -1;
}
$result_arr = json_decode($result, true);
echo $result_arr['id'];
curl_close($ch);
$jsonResponse = file_get_contents('https://api.mercadolibre.com/items/MLB752465575');
$obj = json_decode($jsonResponse);
echo "id: {$obj->id}<br>";
What you did in your code was to json_decode the URL itself. You needed to get the content from the URL, and then decode the content.

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.

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

php search url source code for specific word and then redirect to url

I need the php code for searching the source code of an url for a specific word and if the word exists it will redirect to that url. I have the following code but i don't know how to do the redirect part:
<?php
$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo (stristr ($result, 'specificword')) ? "<div style='text-align:center; color:green'>Online</div>" : "<div style='text-align:center; color:red'>Offline</div>";
?>
UPDATE: with the code you provided, this is my fix:
<?php
$URL = 'http://www.example.com';
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (stristr ($result, 'Domain')) {
header('Location: '. $URL);
}
else{
echo "No matches found";
}
?>
Old answer
If you first want to show ONLINE, you would require something like a META-refresh.
Change the last line to:
echo (CONDITION HERE) '<html>
<head>
<title>Site online</title>
<meta http-equiv="refresh" content="5; url=http://example.com/">
</head>
<body>
<div style="text-align:center; color:green">Online</div>" : "<div style='text-align:center; color:red'>Offline</div>
</body>
</html>';
If you don't need to first show ONLINE and just want to redirect, try:
if (stristr ($result, 'specificword')) {
header('Location: http://www.example.com');
}
Also I suggest to use mb_stristr because it handles Unicode better.
My updated code is as follows:
<?php
$URL = 'http://www.example.com';
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (stristr ($result, 'Domain')) {
header('Location: '. $URL);
}
else{
echo "No matches found";
}
?>
if something == true then
header("location: myurl.com");
exit;
end

Categories