0

I am creating a PHP package that I want anyone to be able to use. I've not done any PHP dev in a few years and I'm unfamiliar with pear and pecl.

The first part of my question is related to Pecl and Pear:
It seems to me that Pear and pecl are updating my computer, rather than doing anything to my code base, which leads me to the assumption that anything I do with them will also need to be duplicated by anyone wanting to use my package. Is that correct?

The 2nd part of my question is specific, I just want to do a simple HTTP (POST) request, and ideally I'd like to do it without any config required by those who use my package.

These are options I'm considering :

  • HTTPRequest seems like the perfect option, but it says "Fatal error: Uncaught Error: Class 'HttpRequest' not found" when I try and use it out of the box, and when I follow these instructions for installing it I get, "autoheader: error: AC_CONFIG_HEADERS not found in configure.in ERROR: `phpize' failed" -- I don't want to debug something crazy like that in order to do a simple HTTP request, nor do I want someone using my package to have to struggle through something like that.

  • I've used HTTP_Request2 via a pear install and it works for me, but there is nothing added to my codebase at all, so presumably this will break for someone trying to use my package unless they follow the same install steps?

  • I know that I can use CURL but the syntax for that seems way over the top for such a simple action (I want my code to be really easy to read)

  • I guess I can use file_get_contents() .. is that the best option?
    and perhaps I'll phrase the 2nd part of my question as :

Is there an approach that is considered best practice for (1) doing a HTTP request in PHP, and (2) for creating a package that is able to be easily used by anyone?

Community
  • 1
  • 1
kris
  • 11,868
  • 9
  • 88
  • 110
  • 1
    i know you said you think curl() is over the top, but i'm not sure why, and that is what i would recommend. the problem with `file_get_contents` is a lot hosts will not have the external wrappers enabled. –  Dec 14 '16 at 02:55
  • Thanks @Dagon, the biggest issue I have with PHP curl, and this is probably due to my being unfamiliar with it, is that I don't know how to see what the actual "curl" command it is doing is (ie. what the command line equivalent) of my code would be, I've had situation where I have a curl command line statement that does what I want, and then I've really struggled converting it into PHP curl code to do the same – kris Dec 14 '16 at 02:58
  • that would be a better question to ask, this one is opinion based and to broad. while curl does have a million options(which is great) sounds like your code would be 3-4 lines http://php.net/manual/en/curl.examples-basic.php –  Dec 14 '16 at 03:00
  • fair point about the HTTP request part of the question; but the part of my question about creating packages that can be used by others without installation I think is an important one ... I couldn't find that information easily by just googling – kris Dec 14 '16 at 03:02
  • 1
    well every host can decide to install curl or not, as well as disable any function - there's no universal approach. –  Dec 14 '16 at 03:04
  • But the file_get_contents() can be expected to just work. I'm going with a curl approach at the moment based on your comments and the answer below from @bugfroggy. I've also posted the specific question about outputting the command line version of a PHP curl statement here : http://stackoverflow.com/questions/41134311/php-curl-converted-to-command-line-equalivalent – kris Dec 14 '16 at 03:22
  • 1
    no it cant be, it only works on http if the fopen wrappers have been enabled. and i know that many shared hosts do not enable them –  Dec 14 '16 at 03:24

1 Answers1

1

This really depends on what you need your request for. While it can be daunting when first learning it, I prefer to use cURL requests most of the time unless all I need to do is query the page with no headers. It becomes pretty readable once you get used to the syntax and the various options in my opinion. When all I need to do is query a page with no headers, I will usually use file_get_contents as this is a lot nicer looking and simpler. I also think most PHP developers can agree with me on this standpoint. I recommend using cURL requests as, when you need to set headers, they're very organized and more popular than messing with file_get_contents.

EDIT
When learning how to do cURL in PHP, the list of options on the documentation page is your friend! http://php.net/manual/en/function.curl-setopt.php

Here's an example of a simple POST request using PHP that will return the response text:

$data = array("arg1" => "val1", "arg2" => true); // POST data included in your query
$ch = curl_init("http://example.com"); // Set url to query 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // Send via POST                                         
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // Set POST data                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response text   
curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/x-www-form-urlencoded"); // send POST data as form data

$response = curl_exec($ch);
curl_close($ch);
robere2
  • 1,689
  • 2
  • 16
  • 26