Failed to open stream: HTTP request failed php

Viewed 29

i am new to php

i am trying to make simple script to scrape product name and price

i am using html dom parser

there are 2 issues that it gives error

Warning: file_get_contents(https://eg.iherb.com/pr/california-gold-nutrition-sport-whey-protein-isolate-1-lb-16-oz-454-g/71031): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in C:\xampp\htdocs\demo\simple_html_dom.php on line 1556

Fatal error: Uncaught Error: Call to a member function find() on null in C:\xampp\htdocs\demo\simple_html_dom.php:1582 Stack trace: #0 C:\xampp\htdocs\demo\index.php(15): simple_html_dom->find('a') #1 {main} thrown in C:\xampp\htdocs\demo\simple_html_dom.php on line 1582

the script couldn't open the links of the products and also i couldnot implement the 2 elements to find them and then echo them

please help me :)

here it is the code

include_once("simple_html_dom.php");

// set target url to crawl
$url = 'https://eg.iherb.com/pr/california-gold-nutrition-sport-whey-protein-isolate-1-lb-16-oz-454-g/71031'; // change this

// open the web page
$html = new simple_html_dom();
$html->load_file($url);



foreach($html->find("price-inner-text") as $price);
foreach($html->find("h1#name") as $productname);

?>
1 Answers

The 403 HTTP status means they know what you are doing a gave you the 403 to shut you down.

I checked out the page URL you used, turned off the browser's javascript and the page rendered but did not not contain product information.
But I still got the HTML and JavaScript code.
Your only hope is that somewhere in the javaScript it contains the data you are looking for.

If the data is available then you need to use curl to get the page. And you must initialize curl's options to make the request look exactly like your browser is making the request.

What you need to do now is turn off your Browser's javascript go to the page. The use the Browser's view source and copy the source in to a text editor and search for the values you want.

This is what the page looked like without javascript.

enter image description here

Related