• 技术文章 >后端开发 >PHP问题

    php curl数据传不过去怎么办

    藏色散人藏色散人2021-09-13 09:34:20原创139

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

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

    php curl数据传不过去怎么办?

    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视频教程

    以上就是php curl数据传不过去怎么办的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php curl
    上一篇:linux yum怎么安装php环境 下一篇:php zend安装配置的方法
    线上培训班

    相关文章推荐

    • PHP curl_init用法是什么• php curl返回错误码60怎么办• php curl怎么输出错误• 如何解决php curl 错误码7的问题

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网