PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php curl 无法post怎么办

藏色散人
藏色散人 原创
2021-09-15 09:50:03 2023浏览

php curl无法post的解决办法:1、打开相应的php代码文件;2、通过“$post_data = "username=bob&key=12345";$response = http_req(...)”方式提交数据即可。

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php curl 无法post怎么办?

php curl post提交数据失败解决方法

代码如下:

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 );

   

推荐学习:《PHP视频教程

<br>
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。