douzachan4578 2016-12-14 02:33
浏览 54
已采纳

如何在PHP中执行HTTP(POST)请求而无需任何配置

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?

  • 写回答

1条回答 默认 最新

  • dongzhuo1498 2016-12-14 02:56
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题