PHP中curl的CURLOPT_POSTFIELDS参数使用细节

CURL确实是一个不错的好工具,不仅在PHP中还是其他的操作系统中,都是一个非常好用的。但是如果你有些参数没有用好的话,那可能会得不到自己理想中的结果。

在通常情况下,我们使用 CURL 来提交 POST 数据的时候,我们已经习惯了这样的写法:

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

但是这样的写法在有时候并不会很好用,可能会得到服务器返回的 500 错误。但是我们尝试在使用 Socket 方式向服务器提交数据的时候,我们会得到非常正确的结果。例如我们在服务器上面存在一个如下的 PHP 文件:

<?php print_r($_SERVER); ?>

当我们采用 CURL 在不注意细节的前提下向服务器发送一些数据,我们可能得到下面这样的结果,这不是我们理想中的结果:

[CONTENT_TYPE] => multipart/form-data; boundary=—————————-f924413ea122

但是如果我们在采用 http_build_query($post_data) 来替代 $post_data 再向这个 PHP 脚本提交数据的时候,我们就会得到和上面不同的结果,这才是我们理想中的结果:

[CONTENT_TYPE] => application/x-www-form-urlencoded

从上面这个例子中不难看出,使用 CURL 并且参数为数据时,向服务器提交数据的时候,HTTP头会发送Content_type: application/x-www-form-urlencoded。这个是正常的网页<form>提交表单时,浏览器发送的头部。而 multipart/form-data 我们知道这是用于上传文件的表单。包括了 boundary 分界符,会多出很多字节。官方的手册上是这样说的:

The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like ‘para1=val1¶2=val2&…' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

使用数组提供 post 数据时,CURL 组件大概是为了兼容 @filename 这种上传文件的写法,默认把 content_type 设为了 multipart/form-data。虽然对于大多数服务器并没有影响,但是还是有少部分服务器不兼容。

经过一番总结最终得出结论:

在没有需要上传文件的情况下,尽量对 post 提交的数据进行 http_build_query 处理,然后再发送出去,能实现更好的兼容性,更小的请求数据包。 

参考代码:

<?php
/**
 * PHP发送Post数据
 *
 * @param string $url 请求url
 * @param array/string $params 发送的参数
 * @return array
 */
function http_post_data($url, $params = array())
{
	if (is_array($params))
	{
		$params = http_build_query($params, null, '&');
	}

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$response = curl_exec($ch);
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);

	return array($httpCode, $response);
}

$url = "http://blog.snsgou.com";
$data = array('a' => 1, 'b' => 2, 'c' => 2);
list($returnCode, $returnContent) = http_post_data($url, $data);
posted @ 2016-07-17 01:32  52php  阅读(28867)  评论(0编辑  收藏  举报