12

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.

selanac82
  • 2,920
  • 4
  • 27
  • 37

2 Answers2

16

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().

drew010
  • 68,777
  • 11
  • 134
  • 162
2

To get the header information in the response.

curlsetopt($ch,CURLOPT_HEADER,true);
arosolino
  • 1,059
  • 8
  • 10