Home > Mobile >  Problem with getting source code of a web page using PHP curl
Problem with getting source code of a web page using PHP curl

Time:12-17

I have absolutely no problem in getting source code of the webpage in my local server with this:

$html = file_get_contents('https://opac.nlai.ir');

And I was also okay on my host using code below until just a few days ago:

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, 'http://opac.nlai.ir');  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);  
$result = curl_exec($curl);

But today I figured out that now the site is using ssl and it is not working with http anymore and force redirect to https. So I did some search & found this as a fix:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($curl CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

The code above works just fine for sites like e.g. "https://google.com" (and any other https sites that I've tried! ) But not for that specific website ("https://opac.nlai.ir") In that case, page takes about a minute to load (!) and finally with var_dump($result) , I get "bool(false)" I don't know how that website can be different from other websites and I really want to know what cause the problem.

Sorry for my English.

CodePudding user response:

Just filling this answer for the records.


As said in question comments, the website where you were trying to get its source code was blocking your requests due your server location area (that executed the requests). It seems it only responds to specific IP location.

The solution verified by @Hesam has been to use cUrl via a proxy IP located in allowed location area, and he found one at least running well.

He followed the instructions found in this other SO post: How ot use cUrl via a proxy

  • Related