0

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);

    curl_close($ch);

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

1
  • Please tell us a little bit more - 2 legged or 3-legged, e.g. is end-user going to authorize API access? Which oAuth protocol version are you going to implement - 1.0a or 2.0? Commented Dec 13, 2011 at 17:59

2 Answers 2

1

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>
<html>
(...)
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Use curl_getinfo to read headers. It is not necessary to set (CURLOPT_HEADER, true) to do this.

For example:

    ...
    curl_setopt($this->ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

    $response = curl_exec($this->ch);
    $httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.