file_get_contents返回:对不起!有些不对劲

时间:2019-02-13 16:31:11

标签: php httprequest file-get-contents

我使用功能file_get_contents从网站获取内容。但是只要看到messege“对不起!出问题了。”

我的代码在这里:

<?php
$kkk = 'https://batdongsan.com.vn/phan-tich-nhan-dinh/thi-truong-can-ho-cao-cap-can-mot-su-sang-loc-khat-khe-ar97716';
$ddd = file_get_contents($kkk);
echo $ddd;    
?>

您能帮我解释一下此错误或任何想法吗

非常感谢您!

1 个答案:

答案 0 :(得分:0)

是的,file_get_contents()返回消息“抱歉!出了点问题。”对我来说也是。使用 PHP CURL 进行API调用。让我们这样尝试吧-

注意:

  

file_get_contents()未检索到的URL,因为它们的   服务器检查请求是否来自浏览器或任何脚本?如果   他们从脚本中发现了请求,只是禁用了页面内容。

     

因此您必须发出类似于浏览器请求的请求。的PHP   卷毛是适合这种工作的选择。参见here

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://batdongsan.com.vn/phan-tich-nhan-dinh/thi-truong-can-ho-cao-cap-can-mot-su-sang-loc-khat-khe-ar97716",
  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;
}