【发布时间】: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.
【问题讨论】: