Php Curl HTTP POST REQUEST使用嵌套键值对设置自定义标题 [英] Php Curl HTTP POST REQUEST set custom header with nested key value pairs
问题描述
如何使用Curl在php中设置此标题?
(CustomInfo元素是数组(嵌套键值对)和AuthenticationInfo元素是数组(嵌套键值对))
< xml bla bla ...>
< Header>
< CustomInfo>
< IsTestMessage> true< / IsTestMessage>
< IsContentCompressed> false< / IsContentCompressed>
< / CustomInfo>
< AuthenticationInfo>
< ApplicationId> SomeId< / ApplicationId>
< VersionId> 0.9< / VersionId>
< RelationId>< / RelationId>
< UserId> SomeUserId< / UserId>
< Password> SomePassword< / Password>
< / AuthenticationInfo>
< / Header>
< Body>
<! - etc ...(actual xml) - >
< / Body>
< / xml bla bla ...>
通常我会这样做:
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,http://www.example.com/process.php);
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ vars); // Body
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
$ headers = array();
$ headers [] ='key:value';
$ headers [] ='key2:value2'; //等等...
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
$ server_output = curl_exec($ ch);
curl_close($ ch);
print $ server_output;
但是当标题包含嵌套键值对时,如何做?
编辑1:
以这种方式完成,但不工作(真正的新的,所以我必须做错了):
$ headers = array();
$ headers [] = array('CustomInfo'=> array(
'IsTestMessage'=>true,
'IsContentCompressed'=>false)
);
$ headers [] = array('AuthenticationInfo'=> array(
'ApplicationId'=>SomeId,
'VersionId'=>0.9,
'RelationId'=>,
'UserId'=>SomeUserId,
'Password'=>SomePassword
)
);
$ headers = serialize($ headers);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
var_dump(curl_getinfo($ ch,CURLINFO_HEADER_OUT));
警告:curl_setopt():必须传递一个对象或一个数组的CURLOPT_HTTPHEADER参数
编辑2:
当我没有序列化$ headers时,我得到:
注意:数组到字符串转换
理想情况下,您应使用CURLOPT_POSTFIELDS,除非这是来自外部API的特定要求
$ myArray = array();
$ myArray [] = array('key'=>'value');
$ myArray [] = array('key2'=> array('value2'=> array('foo'=>'bar')));
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,array('myVars'=> $ vars,'myArray'=> $ myArray)); //身体
然后访问 $ _ POST ['myArray']
警告:标头和POST变量可以注入,因此我也建议您对发件人执行检查(例如
$ _ SERVER ['REMOTE_ADDR'] == 127.0.0.1)
如果这些请求只是来自您自己的服务器
我不能提及访问 $ _ POST
,而且不提及:如何防止PHP中的SQL注入?
How do i set this header in php using Curl? (CustomInfo element is array (nested key value pairs) and AuthenticationInfo element is array(nested key value pair))
<xml bla bla...>
<Header>
<CustomInfo>
<IsTestMessage>true</IsTestMessage>
<IsContentCompressed>false</IsContentCompressed>
</CustomInfo>
<AuthenticationInfo>
<ApplicationId>SomeId</ApplicationId>
<VersionId>0.9</VersionId>
<RelationId></RelationId>
<UserId>SomeUserId</UserId>
<Password>SomePassword</Password>
</AuthenticationInfo>
</Header>
<Body>
<!--etc...(actual xml)-->
</Body>
</xml bla bla...>
Normally i would do:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'key: value';
$headers[] = 'key2: value2';//and so on...
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
But how is this done when the header contains nested key value pairs?
Edit 1:
Done this way but doesnt work (real newby so i must be doing it wrong):
$headers = array();
$headers[] = array('CustomInfo' => array(
'IsTestMessage' => "true",
'IsContentCompressed' => "false")
);
$headers[] = array('AuthenticationInfo' => array(
'ApplicationId' => "SomeId",
'VersionId' => "0.9",
'RelationId' => "",
'UserId' => "SomeUserId",
'Password' => "SomePassword"
)
);
$headers = serialize($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
Warning: curl_setopt(): You must pass either an object or an array with the CURLOPT_HTTPHEADER argument
Edit 2:
When i dont serialize $headers i get:
Notice: Array to string conversion
Ideally, you should use CURLOPT_POSTFIELDS unless this is a specific requirement from an external API
$myArray = array();
$myArray[] = array('key' => 'value');
$myArray[] = array('key2' => array('value2' => array('foo' => 'bar')));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,array('myVars' => $vars, 'myArray' => $myArray)); //Body
Then access it $_POST['myArray']
Warning: Both headers and POST variables can be injected so I'd also recommend you do the check on the sender (e.g
$_SERVER['REMOTE_ADDR'] == 127.0.0.1)
if these requests are only meant to come from your own server
I can't mention accessing $_POST
without also mentioning: How can I prevent SQL-injection in PHP?
这篇关于Php Curl HTTP POST REQUEST使用嵌套键值对设置自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!