flocks flocks - 1 year ago 144
HTTP Question

Handling HTTP headers sent with curl

I'm currently trying to implement oAuth on a server side in order to provide an API for developers. I'm experiencing a very easy issue. I want to be able to handle HTTP headers sent to a script called request.php.

I have no idea how I can do that. I'm a coding a wrapper for clients, and try to make http call on request.php with curl.

$data = array('name' => 'Foo');

$header = array('Content-type: text/plain', 'Content-length: 100');
$ch = curl_init("test");

curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/request.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

$res = curl_exec($ch);

$headers = curl_getinfo($ch);


So, in $headers I received the http responses headers but what I want to do is handling headers received by request.php.

Answer Source

You should use

curl_setopt($s, CURLOPT_HEADER, true);

this will cause $res in you code to have both the headers and the data seperated by 2 CRLF (4 chars in total as defined in HTTP standards).

HTTP Response example,

HTTP/1.0 302 Found
Content-Type: text/html; charset=UTF-8
Content-Length: 11782
Date: Tue, 13 Dec 2011 15:07:19 GMT
Server: GFE/2.0

<!DOCTYPE html>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download