错误CURL和PHP从API读取即将推出的JSON

时间:2016-07-20 18:53:13

标签: php json

我使用以下代码从API读取数据:

$ch = curl_init();    
curl_setopt($ch, CURLOPT_URL, $screenshot_id);
curl_setopt($ch, CURLOPT_HEADER, 0);  // No HTTP headers
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // return the data    
$resultset = curl_exec($ch);
curl_close($ch);

我从API收到这个JSON:

{
    "url":"URL page",
    "images":[
        {
            "url":"URL image",
            "width":1360,
            "height":768,
            "_id":"578fc3d14a3c4103002f99b2"
        },
        {
            "url":"URL image",
            "width":320,
            "height":480,
            "_id":"578fc3d44a3c4103002f99b3"
        }
    ],
    "date":"2016-07-20T18:32:42.046Z",
    "id":"MM7Kl31Rl"
}

第一步是将其转换为对象并循环以提取所需数据,使用以下代码:

$screenshot_url_json = json_decode($resultset, true);
if(count($screenshot_url_json['images']) > 0){
   foreach($screenshot_url_json['images'] as $thumb){
       if($thumb['width'] == 1360 && $thumb['height'] == 768){
            $screenshot_url = $thumb['url'];
       }
   }

问题在于,字段“url”的大部分时间是空的,而其他时候成功恢复。问题是什么? curl执行中缺少一些参数?

但是如果我在浏览器中执行“$ screenshot_id”并按ENTER键,它会成功恢复字符串。 我正在使用的API是:AngularJS - Using $index in ng-options

1 个答案:

答案 0 :(得分:0)

试试这个兄弟

<?php
    function _curl($url,$post="",$usecookie = false,$_sock = false,$timeout = false) {  

        $ch = curl_init();
        if($post) {
            curl_setopt($ch, CURLOPT_POST ,1);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
        }
        if($timeout){
            curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        }
        if($_sock){
                curl_setopt($ch, CURLOPT_PROXY, $_sock);
                curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
        }
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"); 
        if ($usecookie) { 
            curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie); 
            curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);    
        }
        curl_setopt($ch,CURLOPT_HTTPHEADER,array(
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language: en-US,en;q=0.5',
            'Accept-Encoding: gzip, deflate',
            'Connection: keep-alive'
        ));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
        $result=curl_exec ($ch); 
        curl_close ($ch); 
        return $result; 
    }
    $socks5 = ''; // I have got error: "message":"Hourly API access limit exceeded" -- So I need change IP xD
    $cookies = tempnam('cookie',rand(1000000,9999999).'.txt');
    $post = 'url=http://www.google.com/';
    $output = _curl('http://getscreenshots.io/api/ss/'.$url,$post,$cookies,$socks5,'');

    $get_url = json_decode($output);

    $url = str_replace('http://getscreenshots.io/', '', $get_url->url);
    // echo 'http://getscreenshots.io/api/ss/'.$url;


    $output = _curl('http://getscreenshots.io/api/ss/'.$url,'',$cookies,$socks5,'');
    // echo $output;

    $string_decoded = json_decode($output);
    if(count($string_decoded->images)>0)
    {
        foreach($string_decoded->images as $key=>$value)
        {
            if($value->width == 1360 && $value->height == 768){
                echo $value->url.'<br>';
                echo $value->_id;
            }
        }
    }
?>

结果我得到了:

enter image description here

了解更多How to use json_decode