1

I used the function file_get_contents to get content from a website. but just see messege "sorry! something went wrong."

My code here:

<?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;    
?>

Can you help me explain this error or any idea

thank you so much!

Nguyen Loc
  • 11
  • 1

1 Answers1

0

Yes, file_get_contents() returns that msg "sorry! something went wrong." for me also. Make API call using PHP CURL. Let's try like this way-

Note:

URL which is not retrieved by file_get_contents(), because their server checks whether the request come from browser or any script?. If they found request from script they simply disable page contents.

So that you have to make a request similar as browser request. PHP Curl is suitable choice for this kind of job. See 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;
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • Thank you for your answer! But i was missing something. this code can work with any domain. but with this domain, "https://batdongsan.com.vn", it's just return "sorry! Something went wrong". – Nguyen Loc Feb 14 '19 at 02:18
  • @NguyenLoc I guess something is configured on this domain for `file_get_contents()` or `http` request from outside, that's why we're getting that response – A l w a y s S u n n y Feb 14 '19 at 02:21