简体   繁体   中英

php cURL and Instagram Photos

So someone Tweets a link to a photo on Instagram : http://instagr.am/p/QSVkR8LS3H/

This redirects from http://t.co/bOJ4EX2j to http://instagr.am/p/QSVkR8LS3H/ to the actual Instagram page http://instagram.com/p/QSVkR8LS3H/ where the photo resides.

Cool. All that is good and well. Now I want a cURL to follow the tweeted link and download that final page, that contains the < img > of the photo. Script looks basically like this :

 $target = 'http://t.co/bOJ4EX2j';

 $ch = curl_init();

 curl_setopt ($ch, CURLOPT_HTTPGET,        TRUE);
 curl_setopt ($ch, CURLOPT_POST,           FALSE);
 curl_setopt ($ch, CURLOPT_COOKIEJAR,      COOKIE_FILE);   // Defined Constant
 curl_setopt ($ch, CURLOPT_COOKIEFILE,     COOKIE_FILE);
 curl_setopt ($ch, CURLOPT_TIMEOUT,        CURL_TIMEOUT);  // Defined Constant
 curl_setopt ($ch, CURLOPT_USERAGENT,      WEBBOT_NAME);   // Defined Constant
 curl_setopt ($ch, CURLOPT_URL,            $target);       // Target site
 curl_setopt ($ch, CURLOPT_REFERER,        '');            // Referer value
 curl_setopt ($ch, CURLOPT_VERBOSE,        FALSE);         // Minimize logs
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);         // No certificate
 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);          // Follow redirects
 curl_setopt ($ch, CURLOPT_MAXREDIRS,      4);             // Limit redirections to four
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);          // Return in string

# Create return array
$return_array['FILE']   = curl_exec($ch); 
$return_array['STATUS'] = curl_getinfo($ch);
$return_array['ERROR']  = curl_error($ch);

# Close PHP/CURL handle
curl_close($ch);

return $return_array;

Now this script has many more components but that's the just of the cURL part. Now, it does manage to spit back that it landed on the correct final page http://instagram.com/p/QSVkR8LS3H/ ) where the image is - but this is what the $return_array['FILE'] spits out :

 500 Server Error

 An internal server error occurred.

Even when if you navigate to the page in your browser, with cookie's off, and not signed into Instagram (if you were) the page loads completely!

What the hell am I missing that's not allowing this to cURL script to download the Instagram page?! It works on just about every other page I try it! Just not Instagram.com?!

Please someone help me crack this nut of a issue - I'd greatly appreciate any help or insight anyone might have.

If you're running this code on a server and not your local machine, it's possible that there's some misconfigured proxy between the server and Instagram.com. Check the code from your local machine, making sure it also works in a browser on the same machine .

If you get this working and discover, as Justin Wood has said, that you have an HTML page and not the image you wanted, I can help you with some PHP to get the image URL (for which you'd then have to run another cURL request).

As said by many of us, that code seems to work perfectly fine. All i can suggest is that you make sure error reporting is on and then check your PHP error logs in your base directory.

You might also want to check that the cURL module is enabled in PHP.ini.

Not sure what else could be wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM