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.

PHP 中的 curl 函数发送 Post 请求应该注意的几点的更多相关文章

  1. PHP使用curl函数实现多种请求(post,get)

    PHP使用curl函数实现get,post请求 一.CURL介绍 CURL是一个非常强大的开源库,支持很多协议,包括HTTP.FTP.TELNET等,我们使用它来发送HTTP请求.它给我 们带来的好处 ...

  2. PHP中的CURL函数库

    PHP中的CURL函数库(Client URL Library Function) curl_close — 关闭一个curl会话curl_copy_handle — 拷贝一个curl连接资源的所有内 ...

  3. Linux中使用curl命令发送带参数的get请求和post请求

    GET 请求 curl命令 + 请求接口的地址 curl http://**.**.***.**/SeedAgile/SeedApi/querySprintByRequirementNo?parame ...

  4. Ajax在静态页面中向指定url发送json请求获取返回的json数据

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  5. PHP中使用raw格式发送POST请求

    如果请求的参数格式是原生(raw)的内容,应该如何为程序构造一个POST请求函数呢? function http_post($url, $data_string) { $ch = curl_init( ...

  6. C# 代码中 计算某个函数 或WebService 请求花费时间

    /// 计算请求所花费的时间 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start( ...

  7. 採用Android中的httpclient框架发送post请求

    /** * 採用httpclientPost请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得 ...

  8. Android中的httpclient框架发送get请求

    /** * 採用httpclientGet请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到 ...

  9. 使用PHP中的curl发送请求

    使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 初始化连接句柄: 设置CURL选项: 执行并获取结果: 释放VURL连接句柄. 下面的程序片段是 ...

随机推荐

  1. Find- Linux必学的60个命令

    1.作用 find命令的作用是在目录中搜索文件,它的使用权限是所有用户. 2.格式 find [path][options][expression] path指定目录路径,系统从这里开始沿着目录树向下 ...

  2. MapReduce 图解流程超详细解答(2)-【map阶段】

    接上一篇讲解:http://blog.csdn.net/mrcharles/article/details/50465626 map任务:溢写阶段 正如我们在执行阶段看到的一样,map会使用Mappe ...

  3. P1249 最大乘积

    打暴力找规律,都是连续自然数去掉一个 n=int(input()) a=[] cnt=0 i=2 tot=0 ans=1 while tot<=n: tot+=i cnt+=1 a.append ...

  4. day20 作业

    目录 今日作业 1.下面这段代码的输出结果将是什么?请解释. 2.多重继承的执行顺序,请解答以下输出结果是什么?并解释. 3.什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先 ...

  5. 使用edac工具来检测服务器内存故障.

    随着虚拟化,Redis,BDB内存数据库等应用的普及,现在越来越多的服务器配置了大容量内存,拿DELL的R620来说在配置双路CPU下,其24个内存插槽,支持的内存高达960GB.对于ECC,REG这 ...

  6. Device eth0 does not seem to be present, delaying initialization(VM虚拟机restart service出现此错误)

    >从vmware workstation中克隆(clone)了一个redhat6.0的虚拟机,启动之后发现网卡没有启动.于是重启一下network服务,发现提示错误信息“Device eth0 ...

  7. LINUX设置SUID,SGID,Stick bit

    前面介绍过SUID与SGID的功能,那么,如何打开文件使其成为具有SUID与SGID的权限呢?这就需要使用数字更改权限了.现在应该知道,使用数字 更改权限的方式为“3个数字”的组合,那么,如果在这3个 ...

  8. mybatis框架学习:

    一.什么是框架 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题 使用框架的好处: 框架封装了很多的细节,使开发者可以使用极简的方式实现功能 大大提高开发效率 二.三层框架 表现层: 用 ...

  9. 错误号码1130:Host 'XXX' is not allowed to connect to this MySQL server

    今天在linux机器上装了一个mysql,想通过sqlyog远程连接过去,发生了:错误号码1130:Host 'XXX' is not allowed to connect to this MySQL ...

  10. 在rabbitmq操作页面上添加队列、交换器及绑定示图

    1.添加队列 2.添加交换器 3.绑定