Php Curl HTTP POST REQUEST set custom header with nested key value pairs

我的梦境 提交于 2019-12-11 00:35:29

问题


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


回答1:


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?



来源:https://stackoverflow.com/questions/39289474/php-curl-http-post-request-set-custom-header-with-nested-key-value-pairs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!