设为首页 收藏本站
查看: 1163|回复: 0

[经验分享] PHP 中的 curl 函数发送 Post 请求应该注意的几点

[复制链接]

尚未签到

发表于 2017-12-31 08:16:05 | 显示全部楼层 |阅读模式
  public function http_request( $url, $post = '', $timeout = 5 ){
  if( empty( $url ) ){
  return ;
  }
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  if( $post != '' && !empty( $post ) ){
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($post)));
  }
  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
  }
  一开始我并没有留意传递过来的数据是application/json编码的json字符串,我在后台直接用接受application/x-www-form-urlencoded编码格式的数据方式来取传递过来的数据(就是直接用的$_POST方式获取的),结果当然没什么也没有取到了。后来,同事直接改了http_request()方法,直接传递application/x-www-form-urlencoded编码格式的数据过来,我这就没有做更改。
  对于上面的问题,我一直纳闷当时为什么没有拿到传递过来的数据。今天项目基本完工,研究了以下。
  <pphp中的curl()函数进行post请求的时候,传递数据的格式可以有以下几种方式:
  (1):由参数拼接而成的key=>value键值对字符串。形如以下: name=xxx&age=23$sex=1
  这种请求参数默认是按照application/x-www-form-urlencoded进行编码的。
  (2):由参数组成的key=>value键值对数组(只能是一维数组,更高维度的数组会报错)。形如以下格式:
[ name="xxx" , age = 23 , sex = 男 ]

  这种请求参数默认是按照multipart/form-data格式进行编码的。
  上面说了,curl()进行post请求的时候,只能传递一维数组作为传递的参数,那么如果想要传递多维数组需要怎么处理那?有两种方式可以来处理,分别是下面的方式3以及方式4。
  (3):将多维数组进行http_build_query()进行处理,等到一个key=>value键值对格式的字符串。如下面所示:
  $data = [ "msg"=>"这是一条测试数据", "xxx" => "yyyy", "msg_data" => [ "name"=>"sunms", "age"=>23, "sex"=>"男", "content"=>[ 1,2,3 ] ], ];
  将得到以下的字符串:
  msg=这是一条测试数据&xxx=yyyy&msg_data[name]=sunms&msg_data[age]=23&msg_data[sex]=男&msg_data[content][0]=1&msg_data[content][1]=2&msg_data[content][2]=3
  这种方式也是通过application/x-www-form-urlencoded进行编码的,在接收方可以通过$_POST直接获取。
  (4):将多维数组转换为json格式的字符串,对字符串进行application/json格式编码,在接收方通过file_get_contents(“php://input”)或者$GLOBALS[‘HTTP_RAW_POST_DATA’]的方式获取传递过来的json格式的字符串,然后将json格式的字符串转换为数组进行处理。 $data = [];
  $data_string = json_encode($data);
  .....
  //设置header信息
  curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string))); 注意:对于application/json格式编码的数据,$_POST是不能直接获取的,需要通过file_get_contents(“php://input”)或者$GLOBALS[‘HTTP_RAW_POST_DATA’]的方式获取。
  For this tutorial, I am going to go through how to setup your development and production server environments properly for PHP development.
  Development environment windows
  Without having to install a Windows server, which is quite costly, you can install one of the several third-party applications to which will get your server environment up for you.
  Some of these applications are XAMPP and WAMP. And all these third-party applications do the same thing―install Apache, MySQL, and PHP inside the program.
  XAMPP is my personal favorite ― which you can download at www.apachefriends.org ― as it is cross-platform. XAMPP stands for cross-platform, Apache, MariaDB, PHP,Perl. When installing XAMPP, it is very straight forward (and just in case you need to know, you can install Laravel with XAMP ) . Just keep hitting next and agree to everything. However, for the development environment ,you need to install the development tools which just changes the php.ini file to allow errors message display. If error messages are not displayed, go to c:xampp/php/ and open the php.ini file, and make sure that everything under error handling and logging (line 463) has been turned on, then restart your Apache server.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-430191-1-1.html 上篇帖子: linux环境下安装PHP的OpenSSL扩展 下篇帖子: php中ajax调用出错的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表