- 浏览: 346325 次
文章分类
最新评论
-
亚当爱上java:
"给pre-commit添加可执行权限:chmod ...
svn强制要求提交注释--pre-commit钩子 -
rosasyou:
不知道这篇文章是怎么才让人看的?估计不是浏览器兼容问题。 ,与 ...
Zend Framework的DB处理 -
jinjiankang:
$youngest=`svnlook youngest $sv ...
svn备份策略 -
wangxc:
你好,我按照你介绍的方法在创建完weekly_backup.p ...
svn备份策略 -
yangfuchao418:
...
关于memcached的浅见
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: $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. 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. 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.<?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;
}
A Generic REST helper
<?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'
)
);
发表评论
-
图片压缩的一些心得
2010-11-03 11:45 2015http://blog.lizhigang.net/arc ... -
PHP程序员面临的成长瓶颈
2010-10-25 10:06 2024作为Web开发中应用最广泛的语言之一,PHP有着大量的粉丝 ... -
URL重写相关
2010-07-26 18:13 1237初级URL重写指南 http://lamp.lin ... -
关于PHP的mod_rewrite重写模块技术个人觉得最好的一篇文章
2010-07-26 09:30 2276最近手里一个PHP项目要用到mod_rewrite模块,很 ... -
PHP源代码分析- tick(s)
2010-07-24 10:13 1984By Altair, http://www.phpint ... -
php多线程编程
2010-07-21 23:52 1079http://www.cnblogs.com/niniwzw/ ... -
php pcntl_fork和pcntl_fork 的用法
2010-07-21 22:23 2154http://www.jb51.net/article/177 ... -
探讨高并发写入文件内容
2010-07-11 22:48 2838读写相关的问题是永远存在的,文件锁就是为了解决这个问题而做的, ... -
php中,高并发状态下文件的读写
2010-07-11 22:28 8790对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些 ... -
php fsockopen解决办法。
2010-07-06 15:07 3187Author:David | English Ver ... -
用 PHP V5 开发多任务应用程序
2010-07-04 00:39 1238http://www.ibm.com/developerw ... -
git使用总结
2010-07-02 23:51 1374http://zhwen.org/?p=articles/gi ... -
Linux消息队列原理与应用
2010-07-02 18:03 9522http://www.wanglong-neu.cn/ ... -
我说PHPer的水平区分(转载)
2010-07-02 10:55 1480说句实话,写这个真够无 ... -
正则表达式解题经验谈
2010-06-29 22:08 1099正则表达式解题经验谈 ... -
深入浅出之正则表达式
2010-06-28 21:14 805dragon.cnblogs.com/archive/2006 ... -
stream_set_blocking让程序无阻塞
2010-06-28 09:48 1390stream_set_blocking让程序 ... -
什么是Socket?Socket协议的形象描述
2010-06-28 00:11 1594socket的英文原义是“ ... -
完善匹配中文的Php正则表达式
2010-06-11 16:15 2302这段时间试图使用php正 ... -
php反射
2010-06-07 20:14 15241。用途: 该扩展分析php程序,导出或提取出关于类、方法、属 ...
相关推荐
1、使用的是curl库,数据采用http post方式表单递交 2、里面有完整的注释,比较规则的代码结构,适合学习交流 3、是控制台程序 4、我也是用到了curl但网上的资料大部分比较松散,我自己实现后分享给大家,相互学习,...
在C++中使用libcurl进行HTTP POST,我们需要包含头文件`#include <curl/curl.h>`,并确保在编译时链接了libcurl库。下面是一个简单的示例代码,演示如何使用libcurl库实现POST请求: ```cpp #include #include #...
在Qt工程中,使用curl库发送POST请求并处理JSON数据是一项常见的任务,特别是在与Web服务交互时。这里我们将深入探讨如何实现这一功能。 首先,`curl`是一个强大的命令行工具和库,用于传输数据,支持多种协议,...
C++ 实现 HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)。 玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的...
一个PHP curl get post通用类,curl 通用方法 。。get /post 传送数据,设置发送方式 0 get 1 post。具有很好的参考价值,朋友们如果喜欢可以下载,如果有更好的curl类可以发布到我们php中文网,大家一起学习交流。
PHP 基于 curl 模拟 post 提交 json 数据示例 本文主要介绍了 PHP 基于 curl 模拟 post 提交 json 数据操作,通过实例形式分析了 PHP 使用 curl 实现 post 方式提交 json 数据相关操作步骤与留意事项。下面是文章的...
在这个例子中,`Http::post`方法会自动处理文件上传和cURL会话的其他细节。 需要注意的是,目标服务器必须正确处理文件上传请求,通常会有一个接收文件的PHP脚本,如`upload.php`,该脚本应解析`$_FILES`全局变量并...
3. **高级功能**:`php_curl`支持多种HTTP方法(GET、POST、PUT等),可以设置HTTP头,进行POST数据提交,处理cookies,使用代理服务器,以及进行SSL连接。还可以通过设置选项实现自动重定向、超时控制、错误处理等...
一个PHP curl get post通用类,curl 通用方法 。。get /post 传送数据,设置发送方式 0 get 1 post。具有很好的参考价值,朋友们如果喜欢可以下载,如果有更好的curl类可以发布到我们php中文网,大家一起学习交流。
php curl远程post上传
玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的CHttpClient类,该类实现了Htpp和Https的get post方法。 2:...
在本项目中,"HTTP HTTPS POST GET (包含curl版本)"可能包含使用curl命令行执行HTTP/HTTPS的GET和POST请求的示例代码。curl允许通过命令行参数设置请求头、请求体等,非常灵活,适用于自动化脚本和测试。 WinHTTP是...
首先,`PHP POST And Get Class`是一个为了简化HTTP请求的工具,它通过封装cUrl库,使得执行POST和GET请求变得更加简单。cUrl是PHP内置的一个强大库,能够处理各种HTTP协议相关的任务,包括发送数据、获取响应等。在...
主要介绍了PHP基于curl模拟post提交json数据操作,结合实例形式分析了php使用curl实现post方式提交json数据相关操作步骤与注意事项,代码简单实用,需要的朋友可以参考下
在PHP开发过程中,cURL库是一个非常重要的工具,它允许我们执行HTTP请求并与其他网络服务进行交互。然而,有时在Windows环境下,你可能会发现PHP环境中并没有预装cURL扩展,这将导致无法使用cURL相关的函数。本文将...
总结,`curl` 是一个功能强大的工具,不仅可以用来进行 SFTP 文件传输,还能方便地处理 HTTP 的 GET 和 POST 请求,尤其在与 JSON 数据交互时非常方便。通过结合使用不同的参数和工具,你可以构建复杂的网络数据传输...
在PHP开发过程中,使用curl库来执行HTTP请求是一项常用的技术,尤其是当需要处理复杂的网络交互时。本文将详细介绍在使用curl访问特定域名时遇到405 Method Not Allowed错误的解决方法。首先,我们先来理解什么是405...
在PHP中,cURL库是一个强大的工具,用于执行HTTP和其他协议的请求,它允许开发者模拟浏览器行为,如发送POST请求、处理cookies、设置代理等。本文将深入解析PHP下的cURL用法,以便更好地理解和应用。 1. **初始化...
hp的curl封装类用法,以实例形式较为详细的讲述了curl封装类及其使用方法,并总结了GET与POST的用法。 使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll) 开启php ...
在PHP 5.3中,cURL扩展提供了丰富的功能,如文件上传、下载、HTTP POST操作、自定义请求头等。 描述中提到“php5.3X64位支持win2008r2”,这意味着这个cURL扩展是为64位的PHP 5.3编译的,并且适用于Windows Server ...