Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get header information from php curl post request [duplicate]

Tags:

php

curl

I've been searching for hours and can't find anything on this. I'm doing a php curl post request to the sugarsync api and it returns a location in the headers that i need. i have no idea how to get this information. i have to keep it as a post because i post an xml file to their api and all they do is return header information. i have no clue how i can access the location in the headers. according to them i need to put it into another xml file and post that as well. any help is appreciated.

like image 564
selanac82 Avatar asked Jul 25 '12 22:07

selanac82


People also ask

How do you get the cURL response header?

In default mode, curl doesn't display request or response headers, only displaying the HTML contents. To display both request and response headers, we can use the verbose mode curl -v or curl -verbose . In the resulting output: The lines beginning with > indicate request headers.

How do I view response headers in PHP?

Response Headers in cURL There is a simple solution built into cURL if you are only interested in the response headers and not the content of the body. By setting the CURLOPT_HEADER and CURLOPT_NOBODY options to true, the result of curl_exec() will only contain the headers.

How can I get header in PHP?

PHP | get_headers() Function The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request. Parameters: This function accepts three parameters as mentioned above and described below: $url: It is a mandatory parameter of type string. It defines the target URL.

What is curl_exec?

curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.


2 Answers

If you set the curl option CURLOPT_FOLLOWLOCATION, cURL will follow the location redirect for you.

If you want to get the headers, set the option CURLOPT_HEADER to 1, and the HTTP response you get back from curl_exec() will contain the headers. You can them parse them for the location.

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1); // return HTTP headers with response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the response rather than output it

$resp = curl_exec($ch);

list($headers, $response) = explode("\r\n\r\n", $resp, 2);
// $headers now has a string of the HTTP headers
// $response is the body of the HTTP response

$headers = explode("\n", $headers);
foreach($headers as $header) {
    if (stripos($header, 'Location:') !== false) {
        echo "The location header is: '$header'";
    }
}

See all of the options at curl_setopt().

like image 124
drew010 Avatar answered Oct 27 '22 04:10

drew010


To get the header information in the response.

curlsetopt($ch,CURLOPT_HEADER,true);
like image 45
arosolino Avatar answered Oct 27 '22 05:10

arosolino