`
zhangchibang
  • 浏览: 345266 次
社区版块
存档分类
最新评论

HTTP POST from PHP, without cURL

    博客分类:
  • PHP
阅读更多

Update May 2010: This is one of my most popular blog entries, so it seems worthwhile to modernize it a little. I've added an example of a generic REST helper that I've been using in a couple of places below the original do_post_request function in this entry. Enjoy!

I don't think we do a very good job of evangelizing some of the nice things that the PHP streams layer does in the PHP manual, or even in general. At least, every time I search for the code snippet that allows you to do an HTTP POST request, I don't find it in the manual and resort to reading the source. (You can find it if you search for "HTTP wrapper" in the online documentation, but that's not really what you think you're searching for when you're looking).

So, here's an example of how to send a POST request with straight up PHP, no cURL:

<?php
function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

$optional_headers is a string containing additional HTTP headers that you would like to send in your request.

PHP's HTTP wrapper will automatically fill out the Content-Length header based on the length of the $data that you pass in. It will also automatically set the Content-Type to application/x-www-form-urlencoded if you don't specify one in the $optional_headers.

I find this very handy; I don't need to code in redirection logic, HTTP auth handling, user agent setting and so on; they are handled for me by PHP. This works for HTTPS as well, if you have openssl enabled.

You may also want to look into http_build_query() which is a convenience function that allows you to assemble query/post parameters from a PHP variable, applying appropriate escaping. You can find an example of this in the REST helper below.

Kudos to Sara Golemon for both http_build_query and exposing the HTTP context parameters up to userspace.

A Generic REST helper

Many web services offer a REST-ful interface for consuming their data, using GET requests for information retrieval and POST requests for making changes. Below you'll find a helper function that can very easily be used to consume a REST API.

The $url parameter is the HTTP or HTTPS URL for the web service. $params is an associative array of form parameters to pass to the web service; they will be passed as _GET parameters for GET requests or _POST parameters for POST requests. The $verb parameter can be GET or POST (and presumably any other valid HTTP REQUEST verb, such as PUT or DELETE, although I haven't tried those and can't say whether they will work as expected). The $format parameter can be "json" or "xml" and will automatically return a decoded json or XML document, respectively.

I've used simplexml here because it is... simple. You could very easily add a "dom" format to return the object using the richer and more complex DOM API instead.

This function uses the ignore_errors context parameter. Without this set (the default is false), PHP will treat 400 and 500 HTTP status codes as a failure to open the stream and won't return you any data. This is usually what you want when using fopen or file_get_contents, but REST services tend to set the HTTP status to indicate the error andwill usually send back a payload that describes the error. We turn on ignore_errors so that we treat any returned payload as json or xml.

When using POST with REST, take care: PHP's HTTP redirection handler will drop your POST payload if the endpoint issues a redirect. If you experience problems using POST with the function below, it might be due to redirects. Most of the POST calls I've run into issue redirects if the URL is missing a trailing '/' character. In other words, if you experience problems where it seems like your parameters are not being sent in, try appending a '/' to the end of the URL and try it again.

<?php
function rest_helper($url, $params = null, $verb = 'GET', $format = 'json')
{
  $cparams = array(
    'http' => array(
      'method' => $verb,
      'ignore_errors' => true
    )
  );
  if ($params !== null) {
    $params = http_build_query($params);
    if ($verb == 'POST') {
      $cparams['http']['content'] = $params;
    } else {
      $url .= '?' . $params;
    }
  }

  $context = stream_context_create($cparams);
  $fp = fopen($url, 'rb', false, $context);
  if (!$fp) {
    $res = false;
  } else {
    // If you're trying to troubleshoot problems, try uncommenting the
    // next two lines; it will show you the HTTP response headers across
    // all the redirects:
    // $meta = stream_get_meta_data($fp);
    // var_dump($meta['wrapper_data']);
    $res = stream_get_contents($fp);
  }

  if ($res === false) {
    throw new Exception("$verb $url failed: $php_errormsg");
  }

  switch ($format) {
    case 'json':
      $r = json_decode($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as json");
      }
      return $r;

    case 'xml':
      $r = simplexml_load_string($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as xml");
      }
      return $r;
  }
  return $res;
}

// This lists projects by Ed Finkler on GitHub:
foreach (
    rest_helper('http://github.com/api/v2/json/repos/show/funkatron')
    ->repositories as $repo) {
  echo $repo->name, "<br>\n";
  echo htmlentities($repo->description), "<br>\n";
  echo "<hr>\n";
}

// This incomplete snippet demonstrates using POST with the Disqus API
var_dump(
  rest_helper(
    "http://disqus.com/api/thread_by_identifier/",
    array(
      'api_version' => '1.1',
      'user_api_key' => $my_disqus_api_key,
      'identifier' => $thread_unique_id,
      'forum_api_key' => $forum_api_key,
      'title' => 'HTTP POST from PHP, without cURL',
    ), 'POST'
  )
);

You can find more documentation on the HTTP wrapper options in the HTTP and HTTPS page in the PHP manual, more on the GitHub API at github.com, more on the Disqus API and more on Ed Finkler at his blog.

分享到:
评论

相关推荐

    CURL HTTP POST方式表单递交

    1、使用的是curl库,数据采用http post方式表单递交 2、里面有完整的注释,比较规则的代码结构,适合学习交流 3、是控制台程序 4、我也是用到了curl但网上的资料大部分比较松散,我自己实现后分享给大家,相互学习,...

    C++ 实现 HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)

    C++ 实现 HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)。 玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的...

    php中curl的get,post通用类

    一个PHP curl get post通用类,curl 通用方法 。。get /post 传送数据,设置发送方式 0 get 1 post。具有很好的参考价值,朋友们如果喜欢可以下载,如果有更好的curl类可以发布到我们php中文网,大家一起学习交流。

    PHP基于curl模拟post提交json数据示例.docx

    PHP 基于 curl 模拟 post 提交 json 数据示例 本文主要介绍了 PHP 基于 curl 模拟 post 提交 json 数据操作,通过实例形式分析了 PHP 使用 curl 实现 post 方式提交 json 数据相关操作步骤与留意事项。下面是文章的...

    php curl模仿form表单提交图片或文件

    在这个例子中,`Http::post`方法会自动处理文件上传和cURL会话的其他细节。 需要注意的是,目标服务器必须正确处理文件上传请求,通常会有一个接收文件的PHP脚本,如`upload.php`,该脚本应解析`$_FILES`全局变量并...

    php扩展 php_curl

    3. **高级功能**:`php_curl`支持多种HTTP方法(GET、POST、PUT等),可以设置HTTP头,进行POST数据提交,处理cookies,使用代理服务器,以及进行SSL连接。还可以通过设置选项实现自动重定向、超时控制、错误处理等...

    php中curl的get,post通用类.zip

    一个PHP curl get post通用类,curl 通用方法 。。get /post 传送数据,设置发送方式 0 get 1 post。具有很好的参考价值,朋友们如果喜欢可以下载,如果有更好的curl类可以发布到我们php中文网,大家一起学习交流。

    用curl做http post

    在C++中使用libcurl进行HTTP POST,我们需要包含头文件`#include &lt;curl/curl.h&gt;`,并确保在编译时链接了libcurl库。下面是一个简单的示例代码,演示如何使用libcurl库实现POST请求: ```cpp #include #include #...

    php curl远程post上传

    php curl远程post上传

    HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)

    玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的CHttpClient类,该类实现了Htpp和Https的get post方法。 2:...

    HTTP HTTPS POST GET(包含curl版本和winhttp两种实现),修复bug版

    在本项目中,"HTTP HTTPS POST GET (包含curl版本)"可能包含使用curl命令行执行HTTP/HTTPS的GET和POST请求的示例代码。curl允许通过命令行参数设置请求头、请求体等,非常灵活,适用于自动化脚本和测试。 WinHTTP是...

    PHP Post And Get Class,封装Curl,可带cookie,让post/get更简单方便。带例程

    首先,`PHP POST And Get Class`是一个为了简化HTTP请求的工具,它通过封装cUrl库,使得执行POST和GET请求变得更加简单。cUrl是PHP内置的一个强大库,能够处理各种HTTP协议相关的任务,包括发送数据、获取响应等。在...

    PHP基于curl模拟post提交json数据示例

    主要介绍了PHP基于curl模拟post提交json数据操作,结合实例形式分析了php使用curl实现post方式提交json数据相关操作步骤与注意事项,代码简单实用,需要的朋友可以参考下

    curl 实现SFTP上传和url Post Json信息

    总结,`curl` 是一个功能强大的工具,不仅可以用来进行 SFTP 文件传输,还能方便地处理 HTTP 的 GET 和 POST 请求,尤其在与 JSON 数据交互时非常方便。通过结合使用不同的参数和工具,你可以构建复杂的网络数据传输...

    Qt工程中用curl发送post请求,发送json返回json

    在Qt工程中,使用curl库发送POST请求并处理JSON数据是一项常见的任务,特别是在与Web服务交互时。这里我们将深入探讨如何实现这一功能。 首先,`curl`是一个强大的命令行工具和库,用于传输数据,支持多种协议,...

    php curl安装 php中没有编译curl的解决方法for windows

    在PHP开发过程中,cURL库是一个非常重要的工具,它允许我们执行HTTP请求并与其他网络服务进行交互。然而,有时在Windows环境下,你可能会发现PHP环境中并没有预装cURL扩展,这将导致无法使用cURL相关的函数。本文将...

    php采用curl访问域名返回405 method not allowed提示的解决方法

    在PHP开发过程中,使用curl库来执行HTTP请求是一项常用的技术,尤其是当需要处理复杂的网络交互时。本文将详细介绍在使用curl访问特定域名时遇到405 Method Not Allowed错误的解决方法。首先,我们先来理解什么是405...

    php下curl用法详解

    在PHP中,cURL库是一个强大的工具,用于执行HTTP和其他协议的请求,它允许开发者模拟浏览器行为,如发送POST请求、处理cookies、设置代理等。本文将深入解析PHP下的cURL用法,以便更好地理解和应用。 1. **初始化...

    php的Curl处理类

    hp的curl封装类用法,以实例形式较为详细的讲述了curl封装类及其使用方法,并总结了GET与POST的用法。 使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll) 开启php ...

    php5.3php_curl

    在PHP 5.3中,cURL扩展提供了丰富的功能,如文件上传、下载、HTTP POST操作、自定义请求头等。 描述中提到“php5.3X64位支持win2008r2”,这意味着这个cURL扩展是为64位的PHP 5.3编译的,并且适用于Windows Server ...

Global site tag (gtag.js) - Google Analytics