1

In PHP what would the regex be to extract "taken" from below, considering that it is dynamic and is always after status:

HTTP/1.0 200 OK
Date: Sat, 09 Feb 2013 23:07:09 GMT
Accept-Ranges: bytes
Server: Noelios-Restlet-Engine/1.1.7
Content-Type: application/json;charset=UTF-8
Content-Length: 147
X-Cache: MISS from geonisis-2.eurodns.com
X-Cache-Lookup: MISS from geonisis-2.eurodns.com:80
Via: 1.0 geonisis-2.eurodns.com (squid/3.1.10)
Connection: keep-alive

{"service":"availability","domain":"","timestamp":1360451229,"content":{"domainList":[{"status":"taken","name":""}]}}

The following shows that I should be using json decoded. How would one achieve this?

The above in generated using:

$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
3
  • 2
    better to parse the JSON string, and then select from the object Commented Feb 10, 2013 at 10:28
  • 2
    Why not use json_decode? Commented Feb 10, 2013 at 10:28
  • You could remove the header from the response by setting CURLOPT_HEADER to 0 (false) instead of 1 (true). Commented Feb 10, 2013 at 10:37

5 Answers 5

3

Remove the header from the response by changing:

curl_setopt($process, CURLOPT_HEADER, false);

And then decode the JSON string with:

$data = json_decode($curlResponse, true);
Sign up to request clarification or add additional context in comments.

Comments

2

If you discard headers from the response, you can use:

$json = '{"service":"availability","domain":"","timestamp":1360451229,
"content":{"domainList":[{"status":"taken","name":""}]}}';
$data = json_decode($json, TRUE);
echo $data['content']['domainList'][0]['status'];

1 Comment

Disable inclusion of header in the response: curl_setopt($process, CURLOPT_HEADER, FALSE); and you will get only the JSON string
1

Why care about the header? That's a JSON string, just decode it and you'll have an object that you can access easily

in php:

$jsonobj = json_decode('{"service":"availability","domain":"","timestamp":1360451229,    "content":{"domainList":[{"status":"taken","name":""}]}}');

in javascript:

var jsonobj = JSON.parse('{"service":"availability","domain":"","timestamp":1360451229,"content":{"domainList":[{"status":"taken","name":""}]}}');

1 Comment

remove the header from the curl response curl_setopt($process, CURLOPT_HEADER, false); and just parse the response like above
1
$string = '
    HTTP/1.0 200 OK
    Date: Sat, 09 Feb 2013 23:07:09 GMT
    Accept-Ranges: bytes
    Server: Noelios-Restlet-Engine/1.1.7
    Content-Type: application/json;charset=UTF-8
    Content-Length: 147
    X-Cache: MISS from geonisis-2.eurodns.com
    X-Cache-Lookup: MISS from geonisis-2.eurodns.com:80
    Via: 1.0 geonisis-2.eurodns.com (squid/3.1.10)
    Connection: keep-alive

    {"service":"availability","domain":"","timestamp":1360451229,"content":{"domainList":[{"status":"taken","name":""}]}}';

$parts = explode("\n", $string);
$json = end($parts);
$data = json_decode($json);

$status = $data->content->domainList[0]->status; die;

echo $status;

Edit (based on the question update):

Remove the CURLOPT_HEADER line from your cURL request. This would simplify the response and make it easier to parse.

Comments

0

If you need to work with headers, you have two options;

// first: regex
preg_match('~"status":"(.*?)"~i', $return, $match);
// print_r($match);
echo $match[1]; // taken

// second: json encode
$response = explode("\r\n\r\n", $return, 3);
// print_r($response);
$json_object = json_decode($response[2]);
$json_array  = json_decode($response[2], true); // toArray
// echo $json_object->content->domainList[0]->status;
echo $json_array['content']['domainList'][0]['status'];

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.