Php 如何在TransIp web服务器上从Instagram API获取JSON?
我使用以下代码从Instagram帐户获取图像:Php 如何在TransIp web服务器上从Instagram API获取JSON?,php,json,Php,Json,我使用以下代码从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) {
<?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
}
?>
以下在本地工作正常-不知道它是否能在您的麻烦主机上工作。您需要下载最新的
cacert.pem
-请参阅函数中的注释。将所述cecert.pem
复制到您的服务器,并编辑该函数以使用该文件
<?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;
}
?>
“我的网站服务器可能不支持文件内容吗?”
-这肯定是托管技术支持的问题?您是否尝试过使用cURL
在您的实时主机上获取请求?@ProfessorAbronsius cURL不起作用……请参阅上面的代码在处理SSL连接时,您通常需要采取额外的步骤来处理SSL协商file\u get\u contents
将接受一个context
参数,您可以相应地配置该参数。cURL也是如此,教授Abronsius你有例子吗?上面我在stack overflow()上找到的cURL代码,谢谢!那很好!还有,在我的“麻烦”网络主机上
<?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;
}
?>