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.
– Edson Alanis
Really? You tried this solution on your company’s network and it didn’t work? What came up? @Edsonalanis
– Miguel
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
– Edson Alanis
@Edsonalanis, and if you try to access directly through the browser?
– Miguel
if I try to open https://api.github.com/users/nategood in browser works
– Edson Alanis
It’s strange, but then I’m not really seeing what I can, without seeing the request/response headers is difficult @Edsonalanis
– Miguel
how do I get the information you need? I’m learning to work with PHP so I don’t have much knowledge yet.
– Edson Alanis
Do you run a linux @Edsonalanis? Try putting this on the terminal:
curl -v https://api.github.com/users/nategood
– Miguel
No, I use Windows
– Edson Alanis
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
– Miguel
It is, why this site works smoothly, now in my application not.
– Edson Alanis
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
– Miguel
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
– Edson Alanis
@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
– Miguel
will be some network blocking here of the company?
– Edson Alanis
I don’t know, because of the browser you can without problem right? @Edsonalanis
– Miguel
Exactly, via browser works
– Edson Alanis