ajax投稿でウェブサイトのアドレスをphpに送信して取得し、jqueryでサイト内の特定の要素を見つけて、それらの要素のテキストを取得します。h1 h2 pa などのいくつかの要素では正常に機能しますが、すべてのサイトでは機能しません。また、本文テキストとメタ タグを取得できません。つまり、body.text は何も返しません。ここで問題を引き起こしているのは、私の ajax 投稿または php ですか?
これが私のajax投稿です
var dataString = name;
$.ajax({
type: "POST",
url: "senddom.php",
data: {"dataString" : dataString },
dataType: "json",
success: function(response) {
$('body').append("<p> contents of title:" + $(response).find("Title").text()+ "</p>");
$('body').append("<p> contents of meta:" + $(response).find('Meta').text()+ "</p>");
$('body').append("<p> contents of all: " + $(response).find('body').text() + "</p>");
$(response).find('p').each(function() {
$('body').append("<p> contents of p: " + $(this).text() + "</p>");
});
そして、私が学び始めたばかりの私のphp
<?php
$site= $_POST['dataString']; // get data
function curl_get($site){
$useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$data=curl_exec($ch);
curl_close($ch);
return $data;
}
function getdom($site){
$html = curl_get($site);
// Create a new DOM Document
$xml = new DOMDocument();
@$xml->loadHTML($html);
echo json_encode($html);
}
echo getdom($site);
?>