php curl 指定编码,php下使用curl进行多种数据编码方式的POST请求

8aaff7e3b5eaaf93570b2beedd5454b9.png

php使用curl请求数据是很常见的,但是根据HTTP/1.1协议下的POST提交数据编码方式的不同,使用curl函数参数的选择也是有所区别的。

请求报文头header中的Content-Type标记着传输的编码方式供服务端识别,以下根据Content-Type的不同正确使用curl传输数据

一.application/x-www-form-urlencoded方式:

1.普通类似web表单数据:

curl方法:

public function curl_post($data, $url) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST,

"POST");

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,

FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,

FALSE);

curl_setopt($ch, CURLOPT_USERAGENT,

'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');

curl_setopt($ch, CURLOPT_FOLLOWLOCATION,

1);

curl_setopt($ch, CURLOPT_AUTOREFERER, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS,

$data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,

true);

$tmpInfo = curl_exec($ch);

if (curl_errno($ch)) {

echo 'Errno' . curl_error($ch);

}

curl_close($ch);

return $tmpInfo;

}12345678910111213141516171819

处理示例:

请求:

public function test2(){

$template = array(

'touser' => 'test1',

'template_id' => '11',

'url' => 'http://www.test.com',

'topcolor' => '#EEEEEE',

'data' => array('t1','t2','t3')

);

$data = $template;

$url =

"http://127.0.0.1/jytest/test1";

$this->curl_post($data, $url);

}123456789101112

接收处理:

public function test1(){

$res = $_POST;

$content = '';

$content = $content.$res['touser'].', ';

$content = $content.$res['template_id'].',

';

$content = $content.$res['url'].', ';

$content = $content.$res['topcolor'].', ';

$content =

$content.serialize($res['data']).', ';

$filePath = "./test.txt";

file_put_contents($filePath,$content);

}123456789101112

test.txt文件内容是: test1, 11,

http://www.test.com, #EEEEEE, s:5:”Array”;,

可以看出提交的数组无法正确接收

如果要传数组,则要将curl_post方法中的

curl_setopt($ch, CURLOPT_POSTFIELDS,

$data);1

改为

curl_setopt($ch, CURLOPT_POSTFIELDS,

http_build_query($data));1

2.传json,沿用上面的curl_post方法不变,只是将json内容当做字符串内容传递,要有个头部字段名与之对应,方面接收

处理示例:

public function test2(){

//请求方

$template = array(

'touser' => 'test1',

'template_id' => '11',

'url' => 'http://www.test.com',

'topcolor' => '#EEEEEE',

'data' => array('t1','t2','t3')

);

$data['data'] = json_encode($template);

$url =

"http://127.0.0.1/test/test1";

$res = $this->curl_post($data, $url);

print_r(json_decode($res,1));

}123456789101112131415

接收处理:

public function test1(){

//接收处理返回

$data = json_decode($_POST['data'],true);

$content=serialize($data);

$filePath = "./test.txt";

file_put_contents($filePath,$content);

}1234567

test.txt文件内容是:

a:5:{s:6:”touser”;s:3:”test1”;s:11:”template_id”;s:2:”11”;s:3:”url”;s:19:”http://www.test.com“;s:8:”topcolor”;s:7:”#EEEEEE”;s:4:”data”;a:3:{i:0;s:2:”t1”;i:1;s:2:”t2”;i:2;s:2:”t3”;}}

可以看出是数组$template的内容

二.直接application/json方式:

curl设置header将Content-Type改为application/json

curl方法

private function

curl_postjson($data,$urlcon){

$ch = curl_init($urlcon); //请求的URL地址

curl_setopt($ch, CURLOPT_CUSTOMREQUEST,

"POST");

curl_setopt($ch, CURLOPT_POSTFIELDS,

$data);//$data JSON类型字符串

curl_setopt($ch, CURLOPT_RETURNTRANSFER,

true);

curl_setopt($ch, CURLOPT_HTTPHEADER,

array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));

$tmpInfo = curl_exec($ch);

curl_close($ch);

return $tmpInfo;

}12345678910

请求:

public function test2(){

//请求方

$template = array(

'touser' => 'test1',

'template_id' => '11',

'url' => 'http://www.test.com',

'topcolor' => '#EEEEEE',

'data' => array('t1','t2','t3')

);

//不需要用单独的字段名对应json数组,服务端要用file_get_contents('php://input')接收

$data = json_encode($template);

$url =

"http://127.0.0.1/test/test1";

$res =

$this->curl_postjson(json_encode($data), $url);

print_r(json_decode($res,1));

}1234567891011121314151617

接收处理:

public function test1(){

$filePath = "./test.txt";

//不需要使用字段名来接收json数组

$res = file_get_contents('php://input');

$res = json_decode($res,1) ;

$content = serialize($res);

file_put_contents($filePath,$content);

echo json_encode($res);

}12345678910

test.txt文件内容是:

s:110:”{“touser”:”test1”,”template_id”:”11”,”url”:”http:\/\/www.test.com”,”topcolor”:”#EEEEEE”,”data”:[“t1”,”t2”,”t3”]}”;

今天就给大家讲这么多吧,php作为开发类的一个语言,现在受到越来越多人的关注,选择合肥达内,不再孤军奋战,轻轻松松做IT高薪白领。合肥PHP培训带领有明确目标的学子迈向成功之路!

【免责声明】本文系本网编辑部分转载,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与管理员联系,我们会予以更改或删除相关文章,以保证您的权益!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值