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

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

    c++代碼,用curl library來實作http post

    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的get,post通用类.zip

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

    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:...

    php的Curl处理类

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

    php_curl-5.3.13-VC9-x64.zip(php curl模块)

    用来替换WAMP server下的php_curl.dll,解决加载curl报错或无法加载问题。下载解压后,覆盖wamp\bin\php\php5.4.3\ext目录下对应文件,重启apache即可。如果不行请检查: 是否已修改php.ini文件去掉extension=...

    c++封装curl,实现get,post,download

    c++封装了curl的get,post,download

    php扩展 php_curl

    php扩展 php_curl 其中包括libeay32.dll,php_curl.dll和ssleay32.dll是开启php_curl的必备条件

    PHP实现通过CURL上传文件功能示例

    PHP使用CURL上传文件只需发送一个POST请求就可以了,在请求中设置某个字段为需要上传的文件全路径,并且以”@”开头,然后使用CURL把该变量以POST方式发送到服务器,在服务端即可以从超级全局变量$_FILES中取到相应...

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

    php curl模仿form表单提交图片或文件 用的tp5做的例子

    HTTPGetPost.rar_c# post_c++ curl_curl_curl post_visual curl

    curl SSL post访问curl SSL post访问

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

    PHP封装cUrl库,实现post登录,get请求资源。支持cookie。 内附使用方法,公司打卡的小案例。

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

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

    php利用CURL模拟进行微信接口的GET与POST类

    php CURL函数可以模仿用户进行一些操作,如我们可以模仿用户提交数据也可以模仿用户进行网站访问了,下面我们来介绍利用CURL模拟进行微信接口的GET与POST例子,例子非常的简单就两个大家一起看看.

    c++ curl multi http post 方式 并发 异步 vs2008

    基于libcurl官网demo,实现http post 并发 异步 demo。以文件为介质保存post参数,执行后,将失败链接的参数回存此文件。HandleCurlMulti 以轮询方式进行,可以设置每次的吞吐量。待优化的地方为文件存储,如果多...

    php的Curl处理类.zip

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

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

    主要介绍了php采用curl访问域名返回405 method not allowed提示的解决方法,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics