我正在尝试抓取以下网址:“ https://www.petfinder.com/animal-shelters-and-rescues/search/?location=&shelter_name=DREAM动物救援”以使用php curl搜索关键字“ DREAM动物救援”,但返回的是空字符串。
我尝试使用file_get_contents()无法正常工作,我的代码如下;
session_start();
include("includes/db_conn.php");
include ('simple_html_dom.php');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.petfinder.com/animal-shelters-and-rescues/search/?location=&shelter_name=DREAM animal rescue");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$response = curl_exec($curl);
if ($response === FALSE) {
echo "cURL Error: " . curl_error($curl);
$error=1;
} else {
// var_dump($response);
// echo $response;
$error=0;
}
curl_close($curl);
if ($error ==0){
$html = new simple_html_dom();
$html -> load($response);
// echo($response);
var_dump($response);
}
我希望获得搜索结果并进行解析,请任何人,我需要快速帮助
答案 0 :(得分:0)
使用开发者工具栏将初始URL重定向到 https://www.petfinder.com/v2/search/organizations?latitude=13.46256&longitude=144.69282&name_substring=DREAM&page=1 尝试:
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.petfinder.com/v2/search/organizations?latitude=13.46256&longitude=144.69282&name_substring=DREAM&page=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
echo $response;
}