3

I am trying to communicate to an API using cURL. One of the methods require that I pass the value of the ININ-ICWS-CSRF-Token header (ie. WAhtYWxoYXlla1dBY2NvUkRJWCQxZmUxZWFhZS0xZTE0LTQyNGYtYjdhZS0zNmZjN2MxYWJmODBYCjEwLjAuNC4xNjA=) and the Set-Cookie (ie. icws_904586002=bf7c7783-6766-4c4f-862b-48f25a9a3741) so I need to extract them so I can pass them later in my code.

Here is what I did to extract the header and the body from the cURL/API respond:

$respond = curl_exec($ch);

//throw cURL exception
if($respond === false){
    $errorNo = curl_errno($ch);
    $errorMessage = curl_error($ch);

    throw new ApiException($errorMessage, $errorNo);
}    

list($header, $body) = explode("\r\n\r\n", $respond, 2);
echo '<pre>';
print_r($header);
echo '</pre>';

This is the content of the $header value:

HTTP/1.1 201 Created
ININ-ICWS-CSRF-Token: WAhtYWxoYXlla1dBY2NvUkRJWCQxZmUxZWFhZS0xZTE0LTQyNGYtYjdhZS0zNmZjN2MxYWJmODBYCjEwLjAuNC4xNjA=
ININ-ICWS-Session-ID: 904586002
Set-Cookie: icws_904586002=bf7c7783-6766-4c4f-862b-48f25a9a3741; Path=/icws/904586002
Location: /icws/904586002/connection
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-Type: application/vnd.inin.icws+JSON; charset=utf-8
Date: Wed, 06 May 2015 17:13:44 GMT
Server: HttpPluginHost
Content-Length: 237

I would like to get in return results like this

the value of "ININ-ICWS-CSRF-Token" is "WAhtYWxoYXlla1dBY2NvUkRJWCQxZmUxZWFhZS0xZTE0LTQyNGYtYjdhZS0zNmZjN2MxYWJmODBYCjEwLjAuNC4xNjA="
the value of the "cookie" is "ININ-ICWS-CSRF-Token: WAhtYWxoYXlla1dBY2NvUkRJWCQxZmUxZWFhZS0xZTE0LTQyNGYtYjdhZS0zNmZjN2MxYWJmODBYCjEwLjAuNC4xNjA="
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • 2
    http://php.net/manual/en/function.http-parse-headers.php – Barmar May 06 '15 at 17:24
  • Thank you @Barmar I get this `Fatal error: Call to undefined function API\http_parse_headers() ` on the top of my class I have `namespace API;` I am sure it is something I am not importing or doing wrong. what am I missing here? – Jaylen May 06 '15 at 17:32
  • Probably this: "(PECL pecl_http >= 0.10.0)" - this function belongs to pecl extension – Jakub Matczak May 06 '15 at 17:33
  • http://stackoverflow.com/search?q=%5Bphp%5D+http_parse_headers – Barmar May 06 '15 at 17:36
  • if you manually want to do that, then header is one command per line, and first instance of colon is break between key and value. If command is not in one line, then first character will always be blank (space). – Sumit Gupta May 06 '15 at 17:38
  • You need the request or response headers ? When you request the response headers they'll be outputted along with the body, you need to split on the first blank line and get the first part. – Pedro Lobito May 06 '15 at 17:48
  • @Mike: You have `ININ-ICWS-CSRF-Token:` twice in the expected results. I though you wanted to also extract the `Set-Cookie:` header. – Nisse Engström May 09 '15 at 23:03

2 Answers2

3

You can use the http_parse_headers function to parse the headers.

$hdr_array = http_parse_headers($header);

foreach ($hdr_array as $name => $value) {
    echo "The value of '$name' is '$value'<br>";
}

If you don't have http_parse_headers, you can use the code in Pedro Lobito's answer.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
<?php

$myHeader = <<< LOL
HTTP/1.1 201 Created
ININ-ICWS-CSRF-Token: WAhtYWxoYXlla1dBY2NvUkRJWCQxZmUxZWFhZS0xZTE0LTQyNGYtYjdhZS0zNmZjN2MxYWJmODBYCjEwLjAuNC4xNjA=
ININ-ICWS-Session-ID: 904586002
Set-Cookie: icws_904586002=bf7c7783-6766-4c4f-862b-48f25a9a3741; Path=/icws/904586002
Location: /icws/904586002/connection
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-Type: application/vnd.inin.icws+JSON; charset=utf-8
Date: Wed, 06 May 2015 17:13:44 GMT
Server: HttpPluginHost
Content-Length: 237
LOL;

preg_match_all('/(.*?Token): (.*?)\s+/', $myHeader, $matches, PREG_PATTERN_ORDER);
$tokenName = $matches[1][0];
$token = $matches[2][0];

echo <<< LOL
the value of "$tokenName" is "$token"
the value of the "cookie" is "$tokenName: $token"
LOL;

?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268