【问题标题】:Error CURL and PHP reading upcoming JSON from API错误 CURL 和 PHP 从 API 读取即将到来的 JSON
【发布时间】:2016-07-20 18:53:13
【问题描述】:

我使用以下代码从 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 是:Get screenshots from website.

【问题讨论】:

    标签: php json


    【解决方案1】:

    试试这个兄弟

    <?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;
                }
            }
        }
    ?>
    

    我得到的结果:

    阅读更多How to use json_decode

    【讨论】:

    • 我更改了它,但结果是一样的 :( .. 这是恢复的 JSON:"{"url":"URL page","images":[],"date":" 2016-07-20T19:22:01.613Z","id":"7VyMGz4az"}" 如您所见,“图像”属性为空,我不知道为什么..非常感谢您的帮助!
    • @LucianoButtazzoni 好的,让我看看。你的 $screenshot_id 是多少?
    • 在第一步中,它会返回这个 URL,例如“getscreenshots.io/api/ss/MM7Kl31Rl”。这是我再次执行以获取具有所需 URL 的 JSON。如果您在浏览器中执行此 URL,您可以看到正确定义了 images 字段的 JSON!
    • @LucianoButtazzoni 我已经用我得到的结果更新了我的源代码。你能再试一次吗?
    • 这是默认的 URL。正在尝试执行此 URL:getscreenshots.io/api/ss/MM7Kl31Rl。那么,问题出在第一步……谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多