0

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.

Hesam
  • 3
  • 1
  • What debugging have you tried? https://stackoverflow.com/questions/3757071/php-debugging-curl/14436877 – Don't Panic Dec 16 '21 at 22:28
  • @Hesam Looks like they are have problems in their server. It is not your fault. I have tried to open their page from two different IPs via browsers and both got timeout error after around 2 minutes. – masterguru Dec 16 '21 at 22:41
  • @masterguru Yes it seems the server is only responding to specific IP location, so I found the problem! My host should be located in that location... Or is there any PHP curl related solution for that? – Hesam Dec 17 '21 at 09:25
  • Surely using a proxy IP located in the area allowed to browse the site you can still use cUrl. If you found any check this post: [How to use cUrl via a proxy](https://stackoverflow.com/questions/5211887/how-to-use-curl-via-a-proxy) – masterguru Dec 17 '21 at 09:42
  • @masterguru Thanks a lot! It is working now via proxy – Hesam Dec 17 '21 at 11:28
  • @Hesam: Great. Nice to know you fixed it. :-) – masterguru Dec 17 '21 at 11:30

1 Answers1

0

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

masterguru
  • 549
  • 4
  • 10