Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends.I am trying to get a response returned from a url after making a post request but i cannot get the returned response.I am expecting two return parameters in the querystring so that i can get the parameters and complete the process.

The url which am invoking returns some parameters as the reponse.Below is my code.

PHP
<?php
$url = 'https://wwww.example.com/test.aspx?name=test&id=1

$myarray[100]= get_headers($url, 1);
print_r($myarray);
?>



The response which is returned is as following:-

Array ( [100] => Array ( [0] => HTTP/1.1 302 Found [Cache-Control] => Array ( [0] => private [1] => private ) [Content-Type] => Array ( [0] => text/html; charset=utf-8 [1] => text/html ) [Location] => https://www.example.aspx?id=7271&status=00 [Server] => Array ( [0] => Microsoft-IIS/8.5 [1] => Microsoft-IIS/8.5 ) [X-AspNet-Version] => 4.0.30319 [X-Powered-By] => Array ( [0] => ASP.NET [1] => ASP.NET ) [Date] => Array ( [0] => Mon, 12 Oct 2015 13:45:15 GMT [1] => Mon, 12 Oct 2015 13:45:16 GMT ) [Connection] => Array ( [0] => close [1] => close ) [Content-Length] => Array ( [0] => 206 [1] => 171 ) [1] => HTTP/1.1 200 OK [Set-Cookie] => ASPSESSIONIDANANANA3343WES=U3UDDGGFDFFDFD; secure; path=/ ) )


My question is,how can i get the 'location header value' with the above url?I need to get the content in the above response.I need to get the returned reponse in that header.Thanks
Posted

1 solution

Here's how to do this with cURL[^]
PHP
$ch = curl_init();
//Set URL
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

//make the request. Will return your headers and content munged together.
$response = curl_exec($ch);

//then split them apart.
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);


If you're using php 5.5 or better, you can also use guzzle; a restful http client for php.[^]

PHP
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://www.example.com/', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
$header = $res->getHeader('location');
// response
$body = $res->getBody();


and a side note; in PHP,
$myarray[100]='value';
doesn't pre-allocate an array with 100 elements in it. It allocates an array and stores a value in key 100.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900