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

5
  • 2
    just use this king of function : php.net/manual/en/function.preg-match.php Commented Aug 21, 2018 at 15:36
  • Is it always the same format... same order of params? Commented Aug 21, 2018 at 15:39
  • 1
    that regex should do it: (?<=access-token: )[a-z0-9]* Commented Aug 21, 2018 at 15:41
  • 1
    Possible duplicate of Returning header as array using Curl Commented Aug 21, 2018 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 Commented Aug 21, 2018 at 15:42

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the one. Regex is the way forward for this, IMHO!
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;

Comments

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

Comments

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.

1 Comment

But don't use this, use regex.

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.