PHP CURL sometimes work and sometimes not

I have a weird scenario here. I have 3 servers:

1.) http://my-server1/test
--> This server url will only return a json object "test"

2.) http://my-server2/get_request
--> This url will send a request via PHP CURL method

3.) http://mylocal-machine-server/get_request
--> The same as my server2, only that, it is run on my local machine via XAMPP

The get_request method in both second and third server has the ff. simple code to test CURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
curl_exec($ch);

The two servers executed the request successfully and the content of google.com was displayed. Now, I changed the url from google.com to my server 1 url in get_request method for both server 2 and my local server, so it looks like this now:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://my-server1/test');
curl_exec($ch);

I run the get_request method on both the second server and my local server. The get_request on my local server was able to get the "test" json object. However, the get_request on my second server takes a while to load, and when it finished loading, it didn't display anything.

Upvotes: 0

Views: 534

Answers (1)

I found the culprit. The ip address of my second server is not whitelisted on the firewall of my first server (the url where I get the data). The second server time out because it has no access yet to the first server. I just realized this when I use the curl_error suggested by greeflas, and find that the error is connection time out.

Upvotes: 1

Related Questions