【问题标题】:file_get_contents request to Instagram API timing out对 Instagram API 的 file_get_contents 请求超时
【发布时间】:2016-08-29 18:52:48
【问题描述】:

近两年来,我一直在使用相同的方法将我的 instagram 提要放到我的主页上。突然,今天我的整个网站在使用时由于连接超时而崩溃:

file_get_contents('https://api.instagram.com/v1/users/self/media/recent?access_token=' . $token . '&count=8');

我看到 Instagram 最近更新了他们的 API,但我无法从他们那里得到关于此事的回复。当我在浏览器中输入网址时,我会立即收到我的 json。

有其他人经历过吗?

【问题讨论】:

    标签: php instagram file-get-contents instagram-api connection-timeout


    【解决方案1】:

    终于找到了解决办法。我研究它的时间比我希望的要长。希望对你有帮助。

    function insta_rulz($url) {
        try {
        $curl_connection = curl_init($url);
        curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    
        $data = json_decode(curl_exec($curl_connection), true);
        curl_close($curl_connection);
            return $data;
        } catch(Exception $e) {
            return $e->getMessage();
        }
    }
    
    $url = "https://api.instagram.com/v1/users/{USERID}/media/recent/?access_token={ACCES_TOKEN}";
    $results_array = insta_rulz($url);
    
    
    $limit = 12; // provide the limit
    $image_array= array(); // array to store images.
    $userRed_array = array();
        for ($i=0; $i < $limit; $i++) {
            $latest_array = $results_array['data'][$i];
            if($latest_array['images'] > 0) {
                $image_array[$i] = $latest_array['images']['standard_resolution'];
            }
        }
    
        foreach($image_array as $obj){
             array_push($arrList, $obj);
        }
    echo json_encode($arrList);
    

    【讨论】:

    • 虽然问题是关于经验,但我认为你的回答应该在cmets中提到,因为它不是很有见地...
    【解决方案2】:

    Instagram PHP API 工作示例:

    <?php
    $api['url'] = 'https://api.instagram.com/v1/users/USERID?access_token=XXXX';
    $api['urlcontents'] = file_get_contents($api['url']);
    $api['response'] = json_decode($api['urlcontents']);
    echo $api['response']->data->bio;
    ?>
    

    没有 JSON 解码的 API 必须返回如下:

    { "data": { "id": "1574083", "username": "snoopdogg", "full_name": "Snoop Dogg", "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg", "bio": "This is my bio", "website": "http://snoopdogg.com",

    【讨论】:

    • 我的 file_get_contents 在浏览器中输入时返回格式正确的 json。但是在我的代码中实现时给我一个连接超时。
    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 2015-04-23
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多