I'm using TwitterOAuth to access my timeline and get the last three tweets. I call oauth2/token with Curl and it returns a string which contains the bearer access token. The problem is how do I parse the response to get the access_token? I could use String functions, but it seems there should be a better way.
Here is my code:
$headers = array(
"POST /oauth2/token HTTP/1.1",
"Host: api.twitter.com",
"User-Agent: my Twitter App v.1",
"Authorization: Basic ". $base64_encoded_bearer_token ."",
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
"Content-Length: 29"
);
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "https://api.twitter.com/oauth2/token");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$header = curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curlResponse = curl_exec($curl);
curl_close ($curl);
This is the $curlResponse variable:
string(1018) "HTTP/1.1 200 OK cache-control: no-cache, no-store,
must-revalidate, pre-check=0, post-check=0 content-disposition:
attachment; filename=json.json content-length: 153 content-type:
application/json;charset=utf-8 date: Tue, 21 Apr 2015 14:58:59 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT last-modified: Tue, 21 Apr 2015
14:58:59 GMT ml: S pragma: no-cache server: tsa_a set-cookie:
guest_id=v1%3A142962833975637457; Domain=.twitter.com; Path=/;
Expires=Thu, 20-Apr-2017 14:58:59 UTC status: 200 OK strict-transport-
security: max-age=631138519 x-connection-hash:
128fc5244a58ab23aa3b42c1827d6748 x-content-type-options: nosniff x-frame-
options: SAMEORIGIN x-response-time: 10 x-transaction: 073ddea9feba6654
x-tsa-request-body-time: 0 x-twitter-response-tags: BouncerCompliant
x-ua-compatible: IE=edge,chrome=1 x-xss-protection: 1; mode=block
{"token_type":"bearer","access_token":"AAAAAAAAAAAA
AAAAAAAAAGYrfQAAAAAAODa03 sxJgQuYFOyapqOCaSy7mQA%3D
IFQZOFEENVMMTosGadfagztbJyZ7bc7ID8wRENK1exJVw48adM6J"}"