file_get_contents() vs. curl for invoking

2019-06-16 03:11发布

According to the description of the Google Custom Search API you can invoke it using the GET verb of the REST interface, like with the example:

GET https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=lectures

I setup my API key and custom search engine, and when pasted my test query directly on my browser it worked fine, and I got the JSON file displayed to me.

Then I tried to invoke the API from my PHP code by using:

$json = file_get_contents("$url") or die("failed");

Where $url was the same one that worked on the browser, but my PHP code was dying when trying to open it.

After that I tried with curl, and it worked. The code was this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);

Questions:

  1. How come file_get_contents() didn't work and curl did?
  2. Could I use fsocket for this as well?

标签: php rest
3条回答
Emotional °昔
2楼-- · 2019-06-16 03:23

Question 1:

At first you should check ini setting allow_url_fopen, AFAIK this is the only reason why file_get_contents() shouldn't work. Also deprecated safe_mode may cause this.

Oh, based on your comment, you have to add http:// to URL when using with file system functions, it's a wrapper that tells php that you need to use http request, without it function thinks you require to open ./google.com (the same as google.txt).

Question 2:

Yes, you can build almost any cURL request with sockets.

My personal opinion is that you should stick with cURL because:

  • timeout settings
  • handles all possible HTTP states
  • easy and detailed configuration (there is no need for detailed knowledge of HTTP headers)
查看更多
别忘想泡老子
3楼-- · 2019-06-16 03:42

file_get_contents probably will rewrite your request after getting the IP, obtaining the same thing as:

file_get_contents("xxx.yyy.www.zzz/app1",...)

Many servers will deny you access if you go through IP addressing in the request.
With cURL this problem doesn't exists. It resolves the hostname leaving the request as you set it, so the server is not rude in response.
This could be the "cause", too..

查看更多
迷人小祖宗
4楼-- · 2019-06-16 03:44

1) Why are you using the quotes when calling file_get_contents?

2) As it was mentioned in the comment, file_get_contents requires allow_url_fopen to be enabled on your php.ini.

3) You could use fsockopen, but you would have to handle HTTP requests/responses manually, which would be to reinvent the wheel when you have cURL. The same goes for socket_create.

4) Regarding the title of this question: cURL can be more customizable and useful to work with complex HTTP transactions than file_get_contents. Though, it should be mentioned, that working with stream contexts allows you to make a lot of settings for your file_get_contents calls. However, I think cURL is still more complete since it gives you, for instance, the possibility of working with multiple parallel handlers.

查看更多
登录 后发表回答