【问题标题】:How to get JSON from Instagram API on TransIp web server?如何从 TransIp Web 服务器上的 Instagram API 获取 JSON?
【发布时间】:2020-07-07 22:28:53
【问题描述】:

我正在使用此代码从 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 吗?

卷曲方式:

$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));

【问题讨论】:

  • "Is file_get_contents maybe not supported on my website server?" - 这肯定是托管技术支持的问题吗?您是否尝试过使用 cURL 在您的直播主机上获取请求?
  • @ProfessorAbronsius cURL 不工作.....见上面的代码
  • 在处理 SSL 连接时,您通常需要采取额外的步骤来处理 SSL 协商。 file_get_contents 将接受 context 参数,您可以对其进行相应配置。 cURL 也是如此 -
  • @ProfessorAbronsius 你有例子吗?上面我在堆栈溢出时发现的 cURL 代码 (stackoverflow.com/questions/16700960/…)

标签: php json


【解决方案1】:

以下内容在本地运行良好 - 是否可以在您的麻烦主机上运行尚不清楚。您需要下载最新的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;
    }
?>

【讨论】:

  • 谢谢!这很好用!另外,在我的“麻烦”网络主机上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多