file_get_contents请求Instagram API超时

时间:2016-08-29 18:52:49

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

近两年来,我一直使用相同的方法将我的Instagram Feed添加到我的主页上。突然,今天我的整个站点在使用时基于连接超时崩溃:

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

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

还有其他人经历过这个吗?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:-1)

使用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",