0

I'm trying to make a JSON call library. It has the following features.

  • be able to customize the header
  • be able to POST any data to server(including form-encoding (a=1&b=2&c=3) and JSON data chunk)
  • parse the response and return as a JSON object

I searched in other questions and found that there are only two answers.

  • use file_get_contents(). This way is pretty simple and easy to use; however, you cannot do anything more than get the content -- you cannot add headers, cannot use POST. But it is usually supported by all servers.
  • use curl. This way seems powerful and you can do nearly everything with it. The disadvantage is that you have to install libcurl and php-curl support on your server, which means you may not use it on servers that have no curl installed.

So, if I want to develop a common library that can be used on most server, is curl a good choice?

Is there any other ways to do this?


In a short word, I'm looking for a PHP version of urllib2 in python - easy to use, powerful, and reliable.

charlee
  • 1,309
  • 1
  • 11
  • 22
  • curl is a fine choice. It is very widely installed, and very often available as an optional install for managed or hosted systems. – Michael Berkowski Oct 04 '12 at 18:40
  • Thanks Michael. I still remember those painful days when I found my hosting provider did't install curl on my virtual server. So I'm not really sure that curl is widely installed... – charlee Oct 04 '12 at 18:44
  • You *can* add headers to `file_get_contents` but using cURL is much more preferred, more info: http://stackoverflow.com/questions/2107759/php-file-get-contents-and-headers – MacMac Oct 04 '12 at 18:47
  • Thanks @BurningtheCodeigniter. I didn't know that `file_get_contents` can be used in that way. However, as you said, cURL looks much better. – charlee Oct 04 '12 at 18:49
  • you can POST also - see http://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents/2445332#2445332 – tomo7 Dec 04 '14 at 17:06

2 Answers2

1

You have two options: curl and HTTP stream context options. Both can accomplish what you have described; curl might not be installed everywhere, while stream contexts are a core feature and are always available.

Actually, you can also implement your library using sockets, which would be more work but will probably allow you greater control if you need to do weirder things with your requests.

lanzz
  • 42,060
  • 10
  • 89
  • 98
  • I wouldn't recommend using sockets except as a very last resort, as there are lots of details to the HTTP protocol (e.g. chunked encoding, redirects), and you'll have to reinvent lots of wheels. – Barmar Oct 04 '12 at 19:38
-1

As i know, curl is included in mostly on many server, since it supported from 4.0.2 PHP version natively. Also there is a native php function header, which customizes your response's headers by simply using like this -> header('location: index.php'), header('cache-control: ok') and so on. You can view Network Functions section on php.net

Henrik Karlsson
  • 5,559
  • 4
  • 25
  • 42
  • Thank you. It seems curl is a good choice. `header()` has nothing to me, since I want to fetch content from other server, but not to give content to others. – charlee Oct 04 '12 at 18:55