سؤال

Is curl something that's not expected to be installed on servers? I'm working for a small development shop and 99% of the problems that I'm having is regarding curl. Most of the projects that I'm working on involves calling a web API. Most web API's suggests using curl by default since you have to pass in a POST data in the request. Every time I complain to my senior that the server that I'm working on doesn't have curl installed the excuse that I'm always getting is that curl is not needed you can always use file_get_contents.

So the question: is curl something that's not expected to be installed on servers that runs PHP, should I always develop using file_get_contents and not curl? Are there any advantages of using file_get_contents over curl or vise versa?

If it helps, the context here is wordpress plugins, shopify apps, drupal modules and other bits of code that a lot of people can install.

هل كانت مفيدة؟

المحلول

The only way to be sure is to test features that you need. For example, in php, you can use Zend Framework's http client which comes with a socket adapter and a curl adapter. The you can test if curl module was available, if not then just use the socket adapter.

Using file_get_contents() for Api calls is not very convenient if you want to check status code and http headers and there's a whole lot more options you can set with curl.

نصائح أخرى

The Requests library may be a good solution to your problem: http://requests.ryanmccue.info/

This library will take care of falling back to another request method such as file_get_contents() if libcurl is not installed on the server.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top