Solution to the problem that php curl cannot post: 1. Open the corresponding PHP code file; 2. Submit via "$post_data = "username=bob&key=12345";$response = http_req(...)" Just data.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if php curl cannot post?
php curl post failed to submit data solution
The code is as follows:
function http_req($http_type, $method, $url, $data) { $ch = curl_init(); if (strstr($http_type, 'https')) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); } if ($method == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } else { $url = $url . '?' . $data; } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100000);//超时时间 try { $ret = curl_exec($ch); } catch (Exception $e) { curl_close($ch); return json_encode(array('ret' => 0, 'msg' => 'failure')); } curl_close($ch); return $ret; } //第一种提交方式在遇到post_data 中包含@等一些符号时会出现提交失败的情况 $url = "http://localhost/web_services.php"; $post_data = array ("username" => "bob","key" => "12345"); $response = http_req('http', 'post', $url, $post_data ); //第二种提交方式可以避免 $url = "http://localhost/web_services.php"; $post_data = "username=bob&key=12345"; $response = http_req('http', 'post', $url, $post_data );
Recommended learning: "PHP Video Tutorial"
<br/>
The above is the detailed content of What should I do if php curl cannot post?. For more information, please follow other related articles on the PHP Chinese website!