Reputation: 196
I'm using this code for getting images from Instagram account:
<?php
$response = file_get_contents('https://www.instagram.com/fcbarcelona/?__a=1');
$response = json_decode($response, TRUE);
foreach($response['graphql']['user']['edge_owner_to_timeline_media']['edges'] as $img) {
?>
<div class="sbi_item sbi_type_image sbi_new sbi_transition" id="sbi_1231046275994525759_3147458623" data-date="1460972173">
<div class="sbi_photo_wrap">
<img src="<?php echo $img['node']['display_url']; ?>">
</div>
</div>
<?php
}
?>
On my Wamp server everything works fine! But when I upload my code to my website server from TransIp I can not receive the JSON from Instagram.
Is file_get_contents maybe not supported on my website server? How can I check it is? Is there another way to retrieve the JSON?
cURL way:
$url = "https://www.instagram.com/fcbarcelona/?__a=1";
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
Upvotes: 0
Views: 178
Reputation: 33813
The following worked OK locally - whether it'll work on your troublesome host or not is unknown. You'll need to download a copy of the latest cacert.pem
- see the note in the function. Copy said cecert.pem
to your server and edit the function to use that file.
<?php
$url='https://www.instagram.com/fcbarcelona/?__a=1';
$res=curl( $url );
if( $res->info->http_code==200 ){
printf( '<pre>%s</pre>', print_r( json_decode( $res->response ), true ) );
}
function curl( $url=NULL, $options=NULL, $headers=false ){
/*
Get a copy of `cecert.pem` from
https://curl.haxx.se/docs/caextract.html
store on webserver and then edit
the path below as appropriate.
*/
$cacert='c:/wwwroot/cacert.pem';
$vbh = fopen('php://temp', 'w+');
/* Initialise curl request object */
$curl=curl_init();
if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
curl_setopt( $curl, CURLOPT_CAPATH, $cacert );
}
/* Define standard options */
curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_FAILONERROR, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.38 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.38' );
curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $curl, CURLOPT_ENCODING, '' );
curl_setopt( $curl, CURLOPT_VERBOSE, true );
curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
curl_setopt( $curl, CURLOPT_STDERR, $vbh );
/* Assign runtime parameters as options */
if( isset( $options ) && is_array( $options ) ){
foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
}
if( $headers && is_array( $headers ) ){
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
}
/* Execute the request and store responses */
$res=(object)array(
'response' => curl_exec( $curl ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
rewind( $vbh );
$res->verbose=stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $curl );
return $res;
}
?>
Upvotes: 1