#This article introduces the solutions to the problems I encountered when using curl. Hope it helps everyone. First, let’s look at an encapsulated curl function
function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $curl = curl_init();//初始化curl curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页 curl_setopt($curl, CURLOPT_HEADER, 0);//设置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_POST, 1);//post提交方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数 $data = curl_exec($curl);//运行curl curl_close($curl); return $data; }
When called, the return result is bool(false)
The error we get through curl_error($curl) in front of the curl_exec function is also string(0) " " Empty string.
Finally, I found that the interface address of the api I called was of the SSL protocol, and then just added the following two.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
If the address requested by curl contains spaces, it will also return false. Pay special attention to this as well.
I have encountered a return false print curl_error($curl) before and got the following error
string(39) "Problem (2) in the Chunked-Encoded data" bool(false)
The solution to this error is to set the HTTP protocol version used by curl, which is to add the following Sentence
//CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0) //CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)。 curl_setopt($curlp, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
The above is the detailed content of Solution to curl returning false in php. For more information, please follow other related articles on the PHP Chinese website!