0

I've been trying to get json and some header elements using this code: And I was able to print the details that I need but it seems that I can't actually "get" them.

$url = 'http://192.168.254.211:8080/sampleOnCurl/auth/login';
$initCurl = curl_init($url);
curl_setopt_array($initCurl, 
    array(
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        // CURLOPT_NOBODY => true,
        CURLOPT_RETURNTRANSFER => true,
        // CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_POSTFIELDS => $loginData,
        CURLOPT_HEADER => true,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json', 
            'jrr: 7ffed684cbe1fc085b7b47dc4e508e99a5effee9', 
            'slave: 01bdfcc20777907712e97f7bd2faeb978584f317',
            'spoil: ' .$timeStamp
        )
    )
);
$result = curl_exec($initCurl);
print_r($result); 
//prints jrr, slave, spoil
$result = json_decode($result);
// $header = curl_getinfo($initCurl);
curl_close($initCurl);

and this is what I get

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
jrr: 2e278657295bdfae2bc49b0bc6ad38363e9b149b
slave: d77985811796708b89471b4d29a904b224d41dde
spoil: 20130916222842701
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 16 Sep 2013 14:28:42 GMT

{"true":null}

I want to get {"true":null} and convert it to array to check if index is true and get jrr, slave and spoil values .. without using the get method ..

The string that is printed doesn't seem to be in json as well. My guess is that the problem is in my curl options. What could I be missing out?

Here's what I need to do: get the json {"true":null} get the Response Header: jrr, slave, spoil avoid using Get method

thanks

4
  • You can use HEAD to get headers (server will interpret it like GET but will not send content in response). But to get JSON you have to use GET or POST. Commented Sep 16, 2013 at 15:04
  • I can't use it if it is GET because the server will return me an error only POST is allowed. On that note specifically what HEAD ? But to get JSON you have to use GET or POST could please elaborate ? as you can see the reponse I get doesn't seem to be in JSON format.. and I can't access them using $result['jrr'] Commented Sep 16, 2013 at 16:53
  • {"true":null} is valid JSON, what is problem with that? You can also use json_decode($result, true) to force array format. HEAD is next request method (like POST or GET), it gets only headers, but you said that only POST is allowed so you can forget about it in that case. Commented Sep 16, 2013 at 17:04
  • it seems that when I add $result = json_decode($result) before print_r the page doesn't print anything and I think that {"true":null} is part of $result if that makes any sense Commented Sep 16, 2013 at 17:09

2 Answers 2

1

How about something like this:

list($headers, $body) = explode("\r\n\r\n", $result, 2);

function parseSimpleHeaders($headers) {
    $lines = explode("\r\n", $headers);
    $parsed = array();
    foreach($lines as $line) {
        $colon = strpos($line, ':');
        if($colon !== false) {
            $name = trim(substr($line, 0, $colon));
            $value = trim(substr($line, $colon + 1));
            $parsed[$name] = $value;
        }
    }
    return $parsed;
}

$headers = parseSimpleHeaders($headers);
$json = json_decode($body);

echo $headers['jrr'], "\n"; // 2e278657295bdfae2bc49b0bc6ad38363e9b149b
echo $headers['slave'], "\n"; // d77985811796708b89471b4d29a904b224d41dde
echo $headers['spoil'], "\n"; // 20130916222842701
var_dump($json); // object(stdClass)#1 (1) { ["true"] => NULL }

N.B. There's a http_parse_headers() function in pecl_http but I've included my own simplified version parseSimpleHeaders() in case it's not installed.

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

1 Comment

thanks a lot .. I was actually thinking that there's no need to create a function to parse the header and body like Request::header, but I guess it doesn't work when a server is included also since the header being requested is a declaration not a default one. Thanks
0

You are getting response content and header in variable $result so you can't parse it directly.

To get it working explode $result using two new lines.

$result = explode("\n\n", $result);

and now you can access headers string by $result[0] and response content by $result[1] (and parse it using json_decode($result[1], true).

5 Comments

I just tried using $result = explode('\n\n', $result); $result1 = json_decode($result[1],true); print_r($result1); but it gives me an error of undefined offset
@shin-shan You have to use " not '. Only in first case PHP will interpret \n as new line. You should know that, it's basic knowledge.
sorry about that my mistake.. but the result is just the same Undefined offset
Check with var_dump what is in $result before and after explode.
before explode $result is string and this is after explode array(1) { [0]=> ... {"true":null}}

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.