I am trying to get images from an Instagram user (it's our own account). Below is the code I am using, but it does not seem to work. I do have the correct userID and accessToken being fed into the code.
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;
}
$result = fetchData("https://api.instagram.com/v1/users/".$userID."/media/recent/?access_token=".$accessToken);
$theinfo = json_decode($result, true);
foreach ($theinfo->data as $post){
echo $post->images->standard_resolution->url;}
Any suggestion would be greatly appreciated.
I have another way to get user images if you know username
here is link of my github repo to get user images
here is link of repo
following php code is used to get user images
i am using curl & scraping images
not using instagram api
function scrape_insta_user_images($username) {
$insta_source = file_get_contents('https://www.instagram.com/'.$username.'/'); // instagram user url
$shards = explode('window._sharedData = ', $insta_source);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
return $insta_array; // this return a lot things print it and see what else you need
}
$username = 'pakistan'; // user for which you want images
$results_array = scrape_insta_user_images($username);
//echo '<pre>';
//print_r($results_array);
//echo '<pre>';
$limit = 56; // provide the limit thats important because one page only give some images.
$image_array= array(); // array to store images.
for ($i=0; $i < $limit; $i++) {
//new code to get images from json
if(isset($results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i])){
$latest_array = $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node'];
$image_data = '<img src="'.$latest_array['thumbnail_src'].'">'; // thumbnail and same sizes
//$image_data = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes
array_push($image_array, $image_data);
}
}
foreach ($image_array as $image) {
echo $image;// this will echo the images wrap it in div or ul li what ever html structure
}
// for getting all images have to loop function for more pages
// for confirmation you are getting correct images view
//https://www.instagram.com/username
<?
function getPosts($username)
{
if (function_exists('curl_init')) {
$url = "https://www.instagram.com/" . $username . "/?__a=1";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
} else {
return false;
}
}
$arResult["POSTS"] = getPosts("instagram");
?>
<? foreach ($arResult["POSTS"]->graphql->user->edge_owner_to_timeline_media->edges as $key => $post): ?>
<div class="col-md-2 col-sm-3 col-xs-4">
<a href="https://www.instagram.com/p/<?=$post->code?>" target="_blank">
<img src="<?=$post->node->thumbnail_resources[3]->src?>">
<div class="instagram-info">
<div class="instagram-icon instagram-icon--likes">
<span><? echo number_format($post->node->edge_liked_by->count, 0, '.', ' '); ?></span>
</div>
<div class="instagram-icon instagram-icon--comments">
<span><? echo number_format($post->node->edge_media_to_comment->count, 0, '.', ' '); ?> <span class="horizontal-line"></span></span>
</div>
</div>
</a>
</div><? endforeach; ?>
Related
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
}
?>
I am trying to get information from a XML Rest API.
I can get everything but not the images.
I can display all info, but when it comes on images I get 401 error
Failed to load resource: the server responded with a status of 401 ().
My username and password are correct. The only way to get the images display is if I login to this API in a different window. Then all images are displayed.
Am I doing something wrong ? Here is my php code:
function CallAPI($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "usr:psw");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$a = "query SQL";
$asd = $database->query($a);
while($row = sqlsrv_fetch_array($asd)){
echo $url = $row['url']; // I can get this data display
$row['hotel_name'];
$a = CallAPI($url);
$axml = new SimpleXMLElement($a);
$img_link = $axml->item->images;
foreach ($img_link->image as $value) {
echo "<img src='".$value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink')."' class='img-responsive' />";
}
So your problem must be that this link need authentication as well. So try make the call for $value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink') to pass user and pass.
So you code should look like this
while($row = sqlsrv_fetch_array($asd)){
$url = $row['url'];
$row['hotel_name']; // No use of this one, maybe you use it on your end
$a = CallAPI($url);
$axml = new SimpleXMLElement($a);
$img_link = $axml->item->images;
foreach ($img_link->image as $value) {
/*I am adding the link to a variable*/
$imgLinkFromValue = $value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink');
$img = CallAPI($imgLinkFromValue );
/* Now if you still get a link just pass the variable */
echo "<img src='$img->**link or href or anything you get**' class='img-responsive' />";
}
UPDATE
If you getting text or symbols maybe you have to use base64_encode().
Try this :
$img = base64_encode(CallAPI($imgLinkFromValue ));
echo '<img src="data:image/jpeg;base64,' . $img . '" class="img-responsive" />'
You can always change the data inside src to png or gif, depends on what you need.
For more Info about base64_encode function you can use this link.
http://php.net/manual/en/function.base64-encode.php
I want to download the google search big image. Below is my code, which is working fine for download small image from google search.
<?php
include_once('simple_html_dom.php');
set_time_limit(0);
$fp = fopen('csv/search.csv','r') or die("can't open file");
$csv_data = array();
while($csv_line = fgetcsv($fp)) {
for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
$imgname = $csv_line[$i];
$search_query = $csv_line[$i];
$search_query = urlencode(trim($search_query));
$html=file_get_html('http://images.google.com/images?as_q='. $search_query .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y');
$image_container = $html->find('div#rcnt', 0);
$images = $html->find('img');
$image_count = 1; //Enter the amount of images to be shown
$i = 0;
foreach($images as $image){
$srcimg = $image->src;
if($i == $image_count) break;
$i++;
$randname = $imgname.".jpg";
$randname = "Images/".$randname;
file_put_contents("$randname", file_get_contents($srcimg));
}
}
}
?>
Any idea?
This worked for me. simple_html_dom.php wouldn't do the trick since the 'big image' is inside a snippet of JSON near each thumbnail in the DOM.
<?php
$search_query = "Some Keyword"; //change this
$search_query = urlencode( $search_query );
$googleRealURL = "https://www.google.com/search?hl=en&biw=1360&bih=652&tbs=isz%3Alt%2Cislt%3Asvga%2Citp%3Aphoto&tbm=isch&sa=1&q=".$search_query."&oq=".$search_query."&gs_l=psy-ab.12...0.0.0.10572.0.0.0.0.0.0.0.0..0.0....0...1..64.psy-ab..0.0.0.wFdNGGlUIRk";
// Call Google with CURL + User-Agent
$ch = curl_init($googleRealURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686; rv:20.0) Gecko/20121230 Firefox/20.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$google = curl_exec($ch);
$array_imghtml = explode("\"ou\":\"", $google); //the big url is inside JSON snippet "ou":"big url"
foreach($array_imghtml as $key => $value){
if ($key > 0) {
$array_imghtml_2 = explode("\",\"", $value);
$array_imgurl[] = $array_imghtml_2[0];
}
}
var_dump($array_imgurl); //array contains the urls for the big images
die();
?>
I think rather than crawling the page you can you the google custom search api
for more details here the url:
https://developers.google.com/custom-search/json-api/v1/overview
I am trying to create a script that extracts text from a website table and displays it via php. When I run it on this address :
http://lmvz.anofm.ro:8080/lmv/detalii.jsp?UNIQUEJVID=50/01/1150001435/1&judet=50
It turns out empty. Is there something wrong with the code? And how can I fix / improve it?
<?php
include_once('simple_html_dom.php');
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
// Start a cURL resource
$ch = curl_init();
// Set options for the cURL
curl_setopt($ch, CURLOPT_URL, 'http://lmvz.anofm.ro:8080/lmv/detalii.jsp?UNIQUEJVID=50/01/1150001435/1&judet=50'); // target
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // provide a user-agent
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow any redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the result
// Execute the cURL fetch
$result = curl_exec($ch);
// Close the resource
curl_close($ch);
// Output the results
echo $result;
function scraping() {
// create HTML DOM
$html = file_get_html('http://lmvz.anofm.ro:8080/lmv/detalii.jsp?UNIQUEJVID=50/01/1150001435/1&judet=50');
// get article block
if($html && is_object($html) && isset($html->nodes)){
foreach($html->find('/html/body/table') as $article) {
// get title
$item['titlu'] = trim($article->find('/html/body/table/tbody/tr[1]/td/div', 0)->plaintext);
// get body
$item['tr2'] = trim($article->find('/html/body/table/tbody/tr[2]', 0)->plaintext);
$item['tr3'] = trim($article->find('/html/body/table/tbody/tr[3]', 0)->plaintext);
$item['tr4'] = trim($article->find('/html/body/table/tbody/tr[4]', 0)->plaintext);
$item['tr5'] = trim($article->find('/html/body/table/tbody/tr[5]', 0)->plaintext);
$item['tr6'] = trim($article->find('/html/body/table/tbody/tr[6]', 0)->plaintext);
$item['tr7'] = trim($article->find('/html/body/table/tbody/tr[7]', 0)->plaintext);
$item['tr8'] = trim($article->find('/html/body/table/tbody/tr[8]', 0)->plaintext);
$item['tr9'] = trim($article->find('/html/body/table/tbody/tr[9]', 0)->plaintext);
$item['tr10'] = trim($article->find('/html/body/table/tbody/tr[10]', 0)->plaintext);
$item['tr11'] = trim($article->find('/html/body/table/tbody/tr[11]', 0)->plaintext);
$item['tr12'] = trim($article->find('/html/body/table/tbody/tr[12]', 0)->plaintext);
$ret[] = $item;
}
// clean up memory
$html->clear();
unset($html);
return $ret;}
}
// -----------------------------------------------------------------------------
// test it!
$ret = scraping();
foreach($ret as $v) {
echo $v['titlu'].'<br>';
echo '<ul>';
echo '<li>'.$v['tr2'].'</li>';
echo '<li>'.$v['tr3'].'</li>';
echo '<li>'.$v['tr4'].'</li>';
echo '<li>'.$v['tr5'].'</li>';
echo '<li>'.$v['tr6'].'</li>';
echo '<li>'.$v['tr7'].'</li>';
echo '<li>'.$v['tr8'].'</li>';
echo '<li>'.$v['tr9'].'</li>';
echo '<li>'.$v['tr10'].'</li>';
echo '<li>'.$v['tr11'].'</li>';
echo '<li>'.$v['tr12'].'</li>';
echo '</ul>';
}
?>
Because in foreach you use result of finding /html/body/table you should not use full path but ask:
$item['titlu'] = trim($article->find('/tbody/tr[1]/td/div', 0)->plaintext);
$item['tr2'] = trim($article->find('/tbody/tr[2]', 0)->plaintext);
and so on...
To your curl works, you need move
$ch = curl_init();
before first curl_setopt
I'm writing an application to get all the relevant media based to a user or a tag.
I was able to the media but the resolution of the user's profile picture found under data/user/profile_picture is quite poor (around 150*150px).
So my question is : is there anyway to get a user profile's picture in a bigger size ? Here are the queries I use to retrieve the media :
https://api.instagram.com/v1/users/3/media/recent/?access_token=ACCESS-TOKEN
https://api.instagram.com/v1/tags/snow/media/recent?access_token=ACCESS-TOKEN
This gets the 600x600 profile picture:
function Request($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function get_value($username, $att, $accesstoken) {
$url = "https://api.instagram.com/v1/users/search?q=" . $username . "&access_token=" . $accesstoken;
if($result = json_decode(Request($url), true)) {
if ($att == "full_name") {
return preg_replace("/[^A-Za-z0-9 ]/", '', $result['data'][0][$att]);
} elseif ($att == "profile_picture") {
$res = str_replace("s480x480", "s600x600", $result['data'][0][$att]);
$res = str_replace("s320x320", "s600x600", $res);
$res = str_replace("s150x150", "s600x600", $res);
return $res;
} else {
return $result['data'][0][$att];
}
}
}
Example Usage:
$profile_picture = get_value("USERNAME","profile_picture", "ACCESS_TOKEN");