John Davidson

PHP Retrieve the headers of a response to a POST request a key-value pair

0 comments
Message:


I have this php scripts:


$url = "https://mysite/web/session/authenticate";
$data = array (
'params' =>
array (
'db' => 'mydb',
'login' => 'myuserid',
'password' => 'myuserpwd',
),
);

$data = json_encode($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/json\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'method' => 'POST',
'content' => $data,
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

From the server response I need to retrieve a key-value pair (set-cookie) contained in the headers.
Response fron POSTMAN
How can I do?
Thanks



My Answer:
"http://example.com/api";
$data = array("key1" => "value1", "key2" => "value2");

$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

$headers = $http_response_header;

foreach ($headers as $header) {
echo $header . "\n";
}

Rate this post

4 of 5 based on 1320 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us