Timeout when trying to access a HTTPS Rest service via PHP

Asked

Viewed 359 times

1

I am trying to access a Rest service that is on an HTTPS server via PHP and is returning timeout. If I take the URL and put in the browser opens.
I am using Httpful (http://phttpclient.com/), I have used other libraries and the same error occurs.
Now when I access a Rest service that is on an HTTP server works.

Below the code I’m using

$url = "https://api.github.com/users/nategood";
echo "<br>{$url}<br>";
$response = \Httpful\Request::get($url)
        ->strictSSL(true)
        ->send();
echo "<br>";
var_dump($response);

The result of the above execution is:

https://api.github.com/users/nategood

Fatal error: Maximum Execution time of 30 Seconds exceeded in phar://C:/xampp/htdocs/projeto_01/httpful.phar/Httpful/Request.php on line 202

Can someone help me?

2 answers

0


By the analysis I did the API service requires that there is a User-Agent defined (I believe the problem arises from it), this approach does not involve \Httpful\Request and I don’t even think it’s necessary:

$opts = array(
  'http'=>array(
    'header'=> "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n"
  )
);

$context = stream_context_create($opts);
$response = file_get_contents('https://api.github.com/users/nategood', false, $context);

In which the content of $response will be:

{ "login": "nategood", "id": 154115, "avatar_url": "https://avatars.githubusercontent.com/u/154115?v=3", "gravatar_id": "", "url": "https://api.github.com/users/nategood", "html_url": "https://github.com/nategood", "followers_url": "https://api.github.com/users/nategood/followers", "following_url": "https://api.github.com/users/nategood/following{/other_user}", "gists_url": "https://api.github.com/users/nategood/gists{/gist_id}", "starred_url": "https://api.github.com/users/nategood/starred{/Owner}{/Repo}", "subscriptions_url": "https://api.github.com/users/nategood/subscriptions", "organizations_url": "https://api.github.com/users/nategood/orgs", "repos_url": "https://api.github.com/users/nategood/repos", "events_url": "https://api.github.com/users/nategood/events{/Privacy}", "received_events_url": "https://api.github.com/users/nategood/received_events", "type": "User", "site_admin": false, "name": "Nate Good", "company": "Showclix, Inc.", "blog": "http://nategood.com", "location": "Pittsburgh, PA", "email": "[email protected]", "hireable": null, "bio": "CTO @Showclix, Founder of @Firstbytes ", "public_repos": 34, "public_gists": 7, "followers": 124, "following": 25, "created_at": "2009-11-16T23:42:29Z", "updated_at": "2017-01-03T14:06:12Z" }

Then I believe you know what to do with it, ex: json_decode($response, true); to transform into an array etc...

You can also do with Curl:

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
curl_setopt($curl, CURLOPT_URL, 'https://api.github.com/users/nategood');
curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
$response = curl_exec($curl);
curl_close($curl);
  • Thanks for the tip, it worked now, but it worked using my cellular network, on my company’s network does not work.

  • Really? You tried this solution on your company’s network and it didn’t work? What came up? @Edsonalanis

  • Even timeout poblema, the strange thing is that if I connect on the internet through the cellular network works. Warning: file_get_contents(https://api.github.com/users/nategood): failed to open stream: An attempt to connect failed it because the connected component has not answered it correctly ap s a per of time or the established failed because the connected host did not respond. in C: xampp htdocs project_01 test-Rest.php on line 146 Fatal error: Maximum Execution time of 30 Seconds exceeded in C: xampp htdocs project_01 test-Rest.php on line 146

  • @Edsonalanis, and if you try to access directly through the browser?

  • if I try to open https://api.github.com/users/nategood in browser works

  • It’s strange, but then I’m not really seeing what I can, without seeing the request/response headers is difficult @Edsonalanis

  • how do I get the information you need? I’m learning to work with PHP so I don’t have much knowledge yet.

  • Do you run a linux @Edsonalanis? Try putting this on the terminal: curl -v https://api.github.com/users/nategood

  • No, I use Windows

  • And if you try it here: https://www.hurl.it/ I tried it and it worked well @Edsonalanis. I added an alternative that might work

  • It is, why this site works smoothly, now in my application not.

  • Try this alternative I added @Edsonalanis. I even think it’s by putting the ip then Blacklisted on the API server, this can happen if they see that there are too many requests, or they suspect that you are trying something less good

  • C: Users Edson Alanis Desktop Curl-7.46.0-Win64 bin>Curl -v https://api.github.com/users/nategood ː Trying 192.30.253.117... * connect to 192.30.253.117 port 443 failed: Timed out ː Trying 192.30.253.116... * connect to 192.30.253.116 port 443 failed: Timed out * Failed to connect to api.github.com port 443: Timed out * Closing Connection 0 Curl: (7) Failed to connect to api.github.com port 443: Timed out

  • @Edsonalanis, I have no idea what it could be, since in the browser you can no problem, it works without probs, and in your tele also works

  • will be some network blocking here of the company?

  • I don’t know, because of the browser you can without problem right? @Edsonalanis

  • Exactly, via browser works

Show 12 more comments

-1

To test, try to allow the source:

header('Access-Control-Allow-Origin: *'); // libera acesso de todos os sites

If successful, replace the asterisk with the URL of the domain you are using.

header('Access-Control-Allow-Origin: https://www.seu_site.com', false);

Also, check whether the SSL certificate is valid and whether the version of it is supported.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.