简体   繁体   中英

Forbidden error when I am trying to fetch website content

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 ?

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
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM