1

I am trying to fetch webpage content with simple_html_dom() but I get 403 Forbidden.
When I am trying through the browser, I do have access.

$target_url ='http://<URL>';
$html = new simple_html_dom();
$html->load_file($target_url);

I am trying to use Guzzel Package, but I receive this error again.

$client = new \GuzzleHttp\Client();
$result = $client->request('GET', 'http://<URL>');
dd($result->getBody());

What should I do ?

malutki5200
  • 1,092
  • 7
  • 15
MajAfy
  • 3,007
  • 10
  • 47
  • 83
  • @Federkun this is part of my Laravel code, `dd` is advance `var_dump` in laravel : https://laravel.com/docs/5.3/helpers#method-dd – MajAfy Dec 06 '16 at 12:06
  • `403 Forbidden` means that "The server understood the request but refuses to authorize it.". Read the documentation provided by `http://`. Usually you need some sort of access_token. – Federkun Dec 06 '16 at 12:07
  • Thanks @Federkun , but I have access to from my browser, so I think I should add some parameter to my php code for request, isn't it? – MajAfy Dec 06 '16 at 12:09
  • if your browser send a cookie/basic access authentication, then you need to send them with guzzle as well. – Federkun Dec 06 '16 at 12:11

1 Answers1

3

Based on the question it is hard to say what the problem is. There are some possibilities though.

The most likely option is that some headers need to be set. Some websites refuse connections that aren't from a browser, so you could try to emulate a browser and see what the result is.

Another possibility is (as stated by @federkun) is that some cookie data or basic access authentication needs to be set.

Width guzzle you can set headers like this:

 $client->request('GET', '/foo.js', [
    'headers'        => ['Accept-Encoding' => 'gzip'],
    'decode_content' => false
]);
Sven Buis
  • 141
  • 2
  • Thanks but this solution cannot solve my problem, I need that default header, I think `` use cloudeflare DNS protection for DDoS Attacks – MajAfy Dec 06 '16 at 13:05
  • You might want to look at this post: http://stackoverflow.com/questions/11886711/curl-cant-fetch-rss-from-website-because-of-cloudflare – Sven Buis Dec 07 '16 at 08:11