Curl空响应,而浏览器显示json输出


curl empty response while browser display json output

$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://itunes.apple.com/search?term=Clean%20Bandit%20-%20Rather%20Be&entity=song&limit=10&lang=fr_fr');
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(curl_errno($ch))
        echo 'Curl error: '.curl_error($ch);
    $CurResult = curl_exec($ch);
    curl_close($ch);
    echo 'Result:'.$CurResult;
$url = 'https://itunes.apple.com/search?term=Clean%20Bandit%20-%20Rather%20Be&entity=song&limit=10&lang=fr_fr';
$content = file_get_contents($url);
print_r($content);

使用下面的代码来获取响应

from PHP manual

curl_errno()

不返回true或false,如果没有错误,则返回错误号0。所以你要么把条件改成

if(curl_errno($ch)!=0)

或使用curl_error()

if(curl_error($ch)!=''){
    echo "error: ".curl_error($ch);
}
http://se2.php.net/manual/en/function.curl-errno.php