为什么cURL总是返回状态码? [英] Why does cURL always return a status code?
问题描述
我有一些PHP代码调用cURL库。我使用它来获取JSON数据。
I have some PHP code that calls into the cURL library. I'm using it to get JSON data.
我设置cURL选择'CURLOPT_RETURNTRANSFER'为1,但仍然获得状态代码..
I have set cURL opt 'CURLOPT_RETURNTRANSFER' to 1, but still get status code..
代码如下:
<?php
function fetch_page($url)
{
$ch = curl_init();
$array = array(
'include'=>'ayam'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
$return = fetch_page(MY_LINK_URL);
echo json_decode($return);
?>
推荐答案
代码看起来完全正确。尝试 var_dump($ result)
,然后返回它,看看它是什么。
The code looks totally correct. Try var_dump($result)
before returning it to see what it is exactly.
另外,将 CURLOPT_HEADER
设置为1并检查浏览器中输出的视图源;这两个都可以帮助调试的问题。
Also, set CURLOPT_HEADER
to 1 and check the view source of the output in your browser; both of these can help debug the issue. Edit the question and post the results if so we can help more effectively.
更新:由于您使用的是HTTPS,因此也可以添加
Update: Since you are using HTTPS, also add
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
这篇关于为什么cURL总是返回状态码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!