简体   繁体   中英

How to do a HTTP (POST) Request in PHP without requiring any configuration

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM