1

在 PHP 中,考虑到它是动态的并且总是在状态之后,正则表达式从下面提取“采取”的方式是什么:

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);
4

5 回答 5

3

通过更改从响应中删除标头:

curl_setopt($process, CURLOPT_HEADER, false);

然后使用以下命令解码 JSON 字符串:

$data = json_decode($curlResponse, true);
于 2013-02-10T10:36:18.917 回答
2

如果您从响应中丢弃标头,则可以使用:

$json = '{"service":"availability","domain":"","timestamp":1360451229,
"content":{"domainList":[{"status":"taken","name":""}]}}';
$data = json_decode($json, TRUE);
echo $data['content']['domainList'][0]['status'];
于 2013-02-10T10:31:32.183 回答
1

为什么要关心标题?这是一个 JSON 字符串,只需对其进行解码,您将拥有一个可以轻松访问的对象

在 php 中:

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

在 JavaScript 中:

var jsonobj = JSON.parse('{"service":"availability","domain":"","timestamp":1360451229,"content":{"domainList":[{"status":"taken","name":""}]}}');
于 2013-02-10T10:33:01.920 回答
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 行。这将简化响应并使其更易于解析。

于 2013-02-10T10:35:04.473 回答
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'];
于 2013-02-10T10:48:50.157 回答