0
<?php 

$curl = curl_init();

    curl_setopt_array($curl, array(
  CURLOPT_URL => "",
  //return the transfer as a string
  CURLOPT_RETURNTRANSFER => true,
  //enable headers
  CURLOPT_HEADER => true,
  //get only headers
  CURLOPT_NOBODY => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "password: ",
    "username: "
  ),
));
// $response contains the output string
$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);

print_r($response);

?>

I am trying to take $response and truncate data from it.My output of $response is

HTTP/1.1 200 OK Server: Apache-Coyote/1.1 access-token: a3581f021476e4fb406c6b7c79e4095cece5d0c6 token-expiration-hours: 24 Content-Type: application/json Content-Length: 0 Date: Tue, 21 Aug 2018 14:12:27 GMT

I would like $response to just be the value of the access-token a3581f021476e4fb406c6b7c79e4095cece5d0c6

Dk 68 61 63 6b
  • 65
  • 1
  • 3
  • 11
  • 2
    just use this king of function : http://php.net/manual/en/function.preg-match.php – Inazo Aug 21 '18 at 15:36
  • Is it always the same format... same order of params? – Adam Aug 21 '18 at 15:39
  • 1
    that regex should do it: `(?<=access-token: )[a-z0-9]*` – Jeff Aug 21 '18 at 15:41
  • 1
    Possible duplicate of [Returning header as array using Curl](https://stackoverflow.com/questions/10589889/returning-header-as-array-using-curl) – iainn Aug 21 '18 at 15:41
  • 1
    You're just looking to access an HTTP header by its name - you aren't the first person with this problem – iainn Aug 21 '18 at 15:42

4 Answers4

1

One option is to use regex to find the access token:

preg_match('/access-token: ([^\s]+) /', $response, $matches);
vaR_dump($matches[1]); //a3581f021476e4fb406c6b7c79e4095cece5d0c6

This will match any string after the string access-token, up to any whitespace. Just make sure the access-token is not the last item in the headers as there probably won't be any whitespace...

Assumption: the access token will never contain any white space.

Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
1

Using this Regular Expression (?<=access-token: )[a-z0-9]* you can get everything after 'access_token: ' until the next space:

<?php

$header = "HTTP/1.1 200 OK Server: Apache-Coyote/1.1 access-token: a3581f021476e4fb406c6b7c79e4095cece5d0c6 token-expiration-hours: 24 Content-Type: application/json Content-Length: 0 Date: Tue, 21 Aug 2018 14:12:27 GMT";
preg_match('/(?<=access-token: )[a-z0-9]*/', $header, $matches);
$access_token = $matches[0];

echo $access_token;
Jeff
  • 6,895
  • 1
  • 15
  • 33
1

Another option is to explode by whitespace, find the key for 'access-token', and the value will be the next in the array:

$data = explode(' ', $response);
$key = array_search('access-token:', $data); //5
var_dump($data[$key + 1]); //a3581f021476e4fb406c6b7c79e4095cece5d0c6
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
0

Use regex like Inazo suggests as it's a better solution, but you can do:

$data = 'HTTP/1.1 200 OK Server: Apache-Coyote/1.1 access-token: a3581f021476e4fb406c6b7c79e4095cece5d0c6 token-expiration-hours: 24 Content-Type: application/json Content-Length: 0 Date: Tue, 21 Aug 2018 14:12:27 GMT';

$explodedData = explode("access-token: ", $data);
$explodedData2 = explode(" token-expiration-hours:", $explodedData[1]);

echo $explodedData2[0]; //contains access token.
Adam
  • 1,294
  • 11
  • 24