2013-02-10 14 views
1

在PHP將正則表達式是什麼來提取「採取」自下而上,考慮到它是動態的,總是狀態後:正則表達式來提取這個字符串或使用JSON解碼

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":""}]}} 

以下介紹我應該使用json解碼。如何實現這一目標?

以上使用產生:

$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); 
+2

更好地解析JSON字符串,然後從對象 – 2013-02-10 10:28:09

+2

選擇爲什麼不使用[json_decode(http://php.net/manual/en/function.json-decode.php)? – 2013-02-10 10:28:12

+0

你是怎麼做到的? – RSM 2013-02-10 10:29:13

回答

3

通過改變拆下響應頭:

curl_setopt($process, CURLOPT_HEADER, false); 

然後解碼JSON字符串:

$data = json_decode($curlResponse, true); 
2

如果從響應丟棄頭,您可以使用:

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

請看更新的問題。 – RSM 2013-02-10 10:36:21

+0

禁止在響應中包含頭文件:'curl_setopt($ process,CURLOPT_HEADER,FALSE);'你只會得到JSON字符串 – Ranty 2013-02-10 10:53:02

1

何必在乎頭?這是一個JSON字符串,只是對其進行解碼,你就會有一個對象,你可以在PHP訪問容易

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

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

請查看更新後的問題。 – RSM 2013-02-10 10:36:44

+0

從curl響應'curl_setopt($ process,CURLOPT_HEADER,false)'中刪除標頭;'並且只是解析類似 – itsid 2013-02-10 10:48:56

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; 

編輯(根據問題更新):

從您的cURL請求中刪除CURLOPT_HEADER行。這將簡化響應並使其更易於解析。

+0

的響應,請參閱更新的問題。 – RSM 2013-02-10 10:35:51

0

如果您需要頭的工作,你有兩個選擇;

// 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'];