我使用curl从蒸汽中获取一个关于库存的json,有时它可以工作但有时它不会。
我认为这是经常要求的
有人有解决方法吗?
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://steamcommunity.com/profiles/'.$id.'/inventory/json/730/2/');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$res = curl_exec($curl_handle);
if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
$dec = json_decode($res, true);
curl_close($curl_handle);
这是我使用的卷曲
答案 0 :(得分:0)
连接超时时间较短,尝试更多值,如10秒。 如果没有解决,过去的异常错误可以获得更多帮助。
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
答案 1 :(得分:0)
尝试使用代码显示标题并找到问题:
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://steamcommunity.com/profiles/'.$id.'/inventory/json/730/2/');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
$res = curl_exec($curl_handle);
print_r(curl_getinfo($curl_handle));
if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
$dec = json_decode($res, true);
curl_close($curl_handle);