1

I do an HTTP POST to a server and I am trying to get the value of session_token out of the results below how can i do that?

My post:

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: POST','Content-Type:application/x-www-form-urlencoded','Content-Length: ' . strlen(http_build_query($data))));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec( $ch );
print_r($response);

the response:

HTTP/1.1 200 OK
Date: Tue, 07 Jul 2015 19:12:25 GMT
Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i mod_wsgi/3.5 Python/2.7.8
Content-Length: 180
Content-Type: application/json

{"guid": null, "session_token": "0kndD67A0dptosqodpSuCUoAsrNxTxnMqme29Grkx0sKaXEKH3wYAis6arOkH4ETHf6ytC8UNotBhwsPM61jJWqnX1mXbhBFlJI8z56yBA6dPUVaynta0LvrNUgZxwc5", "success": true}

I have tried json_decode but that returns null.

5
  • 1
    and where/how do you get this? If that's being posted to you, then you'd have to read the json from php://input. Commented Jul 7, 2015 at 19:18
  • sorry for the lack of data. edited with more info. Commented Jul 7, 2015 at 19:19
  • 2
    turn off the header/header_out stuff that's sticking the response headers into the rest of the response body, which means that whole string isn't json. it's a glob of text with some json at the end. Commented Jul 7, 2015 at 19:22
  • that was it, make an answer an i will accept Commented Jul 7, 2015 at 19:23
  • There is no need to manually set Content-Type: application/x-www-form-urlencoded and Content-Length. Because of curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));, curl sets them for you. Commented Jul 7, 2015 at 19:32

2 Answers 2

2

Turn off/remove these options:

curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

They're causing the http response headers to be included in $response, which makes your response be "random http-related text + json", instead of just "json".

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

1 Comment

When the response body is not encoded as JSON as expected, that "random http response headers" contains the explanation why. Sure, it is useless when everything works correctly.
0

Because of curl_setopt($ch, CURLOPT_HEADER, 1);, curl_exec() returns the complete response message (headers and body). You can either remove the CURLOPT_HEADER option (and receive only the body) or leave the request as is and parse the returned content to identify the headers (if you need to verify them) and the body. Or you can configure curl to return the headers in an already open file and get both the headers and the body but already split.

For the second option (let the request as is and parse the returned content to separate the headers for the body) you can use curl_getinfo() to find out the size of the response headers:

$response = curl_exec($ch);
$info     = curl_getinfo($ch);

// Split the headers and the body
$headers = substr($response, 0, $info['header_size'];
$body    = substr($response, $info['header_size']);

// The body is encoded as JSON (should verify this in the headers)
$pieces = json_decode($body, TRUE);

// Here, if $pieces is not NULL (it happens when json_decode() fails)
// it should be
//     array(
//         'guid' => NULL,
//         'session_token' => '0kndD67A0dptosqodpSuCUoAsrNxTxnMqme29Grkx0sKaXEKH3wYAis6arOkH4ETHf6ytC8UNotBhwsPM61jJWqnX1mXbhBFlJI8z56yBA6dPUVaynta0LvrNUgZxwc5',
//         'success' => TRUE,
//     )

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.