我想做的是使用DOMDocument从正文中获取文档标题
e-g“ document_title”:“ iOS编程要点”,
此代码的结果是我在$ doc_name变量上获得了一个空值。我希望结果为"iOS Programming Nuts and bolts"
编辑:我正在尝试从html源获取json数据(例如:document_description,document_title等。)
源代码:
到目前为止,我有
请帮助朋友。
<?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";
}
?>