PHP | DOMDocument-从正文获取JSON数据

时间:2018-08-04 11:54:27

标签: javascript php html json domdocument

我想做的是使用DOMDocument从正文中获取文档标题

e-g“ document_title”:“ iOS编程要点”,

此代码的结果是我在$ doc_name变量上获得了一个空值。我希望结果为"iOS Programming Nuts and bolts"

编辑:我正在尝试从html源获取json数据(例如:document_description,document_title等。)

源代码:

Please check this image

到目前为止,我有

请帮助朋友。

<?php 

$doc_url = "https://www.scribd.com/book/281016724/iOS-Programming-Nuts-and-bolts";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$doc_url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if($http=="200") {
  $doc = new DOMDocument();
  $doc->loadHTML(htmlspecialchars($result));
  $xpath = new DOMXPath($doc);
  $js = $xpath->query('//body/script[@type="text/javascript"]')->item(0)->nodeValue;
  $start = strpos($js, '{');
  $end = strrpos($js, '),');
  $json = substr($js, $start, $end - $start);
  $data = json_decode($json, true);
  $doc_name = $data["document_title"];
}
else
{
    echo "ERROR";
}

?>    

0 个答案:

没有答案