[英]How to get JSON from Instagram API on TransIp web server?
我正在使用此代碼從 Instagram 帳戶獲取圖像:
<?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
}
?>
在我的 Wamp 服務器上一切正常! 但是當我從TransIp將代碼上傳到我的網站服務器時,我無法從 Instagram 接收 JSON。
我的網站服務器可能不支持 file_get_contents 嗎? 我怎樣才能檢查它是? 還有其他方法可以檢索 JSON 嗎?
cURL方式:
$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));
以下在本地運行良好 - 它是否可以在您的麻煩主機上運行尚不清楚。 您需要下載最新的 cacert.pem 的副本 - 請參閱cacert.pem
中的注釋。 將cecert.pem
復制到您的服務器並編輯 function 以使用該文件。
<?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;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.