- 浏览: 338936 次
文章分类
最新评论
-
亚当爱上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 1950http://blog.lizhigang.net/arc ... -
PHP程序员面临的成长瓶颈
2010-10-25 10:06 1973作为Web开发中应用最广泛的语言之一,PHP有着大量的粉丝 ... -
URL重写相关
2010-07-26 18:13 1148初级URL重写指南 http://lamp.lin ... -
关于PHP的mod_rewrite重写模块技术个人觉得最好的一篇文章
2010-07-26 09:30 2180最近手里一个PHP项目要用到mod_rewrite模块,很 ... -
PHP源代码分析- tick(s)
2010-07-24 10:13 1936By Altair, http://www.phpint ... -
php多线程编程
2010-07-21 23:52 982http://www.cnblogs.com/niniwzw/ ... -
php pcntl_fork和pcntl_fork 的用法
2010-07-21 22:23 2099http://www.jb51.net/article/177 ... -
探讨高并发写入文件内容
2010-07-11 22:48 2787读写相关的问题是永远存在的,文件锁就是为了解决这个问题而做的, ... -
php中,高并发状态下文件的读写
2010-07-11 22:28 8741对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些 ... -
php fsockopen解决办法。
2010-07-06 15:07 3142Author:David | English Ver ... -
用 PHP V5 开发多任务应用程序
2010-07-04 00:39 1181http://www.ibm.com/developerw ... -
git使用总结
2010-07-02 23:51 1279http://zhwen.org/?p=articles/gi ... -
Linux消息队列原理与应用
2010-07-02 18:03 9443http://www.wanglong-neu.cn/ ... -
我说PHPer的水平区分(转载)
2010-07-02 10:55 1416说句实话,写这个真够无 ... -
正则表达式解题经验谈
2010-06-29 22:08 1052正则表达式解题经验谈 ... -
深入浅出之正则表达式
2010-06-28 21:14 761dragon.cnblogs.com/archive/2006 ... -
stream_set_blocking让程序无阻塞
2010-06-28 09:48 1345stream_set_blocking让程序 ... -
什么是Socket?Socket协议的形象描述
2010-06-28 00:11 1545socket的英文原义是“ ... -
完善匹配中文的Php正则表达式
2010-06-11 16:15 2251这段时间试图使用php正 ... -
php反射
2010-06-07 20:14 14811。用途: 该扩展分析php程序,导出或提取出关于类、方法、属 ...
相关推荐
c++代碼,用curl library來實作http post
1、使用的是curl库,数据采用http post方式表单递交 2、里面有完整的注释,比较规则的代码结构,适合学习交流 3、是控制台程序 4、我也是用到了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 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:...
hp的curl封装类用法,以实例形式较为详细的讲述了curl封装类及其使用方法,并总结了GET与POST的用法。 使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll) 开启php ...
用来替换WAMP server下的php_curl.dll,解决加载curl报错或无法加载问题。下载解压后,覆盖wamp\bin\php\php5.4.3\ext目录下对应文件,重启apache即可。如果不行请检查: 是否已修改php.ini文件去掉extension=...
c++封装了curl的get,post,download
php扩展 php_curl 其中包括libeay32.dll,php_curl.dll和ssleay32.dll是开启php_curl的必备条件
PHP使用CURL上传文件只需发送一个POST请求就可以了,在请求中设置某个字段为需要上传的文件全路径,并且以”@”开头,然后使用CURL把该变量以POST方式发送到服务器,在服务端即可以从超级全局变量$_FILES中取到相应...
php curl模仿form表单提交图片或文件 用的tp5做的例子
curl SSL post访问curl SSL post访问
PHP封装cUrl库,实现post登录,get请求资源。支持cookie。 内附使用方法,公司打卡的小案例。
主要介绍了PHP基于curl模拟post提交json数据操作,结合实例形式分析了php使用curl实现post方式提交json数据相关操作步骤与注意事项,代码简单实用,需要的朋友可以参考下
php CURL函数可以模仿用户进行一些操作,如我们可以模仿用户提交数据也可以模仿用户进行网站访问了,下面我们来介绍利用CURL模拟进行微信接口的GET与POST例子,例子非常的简单就两个大家一起看看.
基于libcurl官网demo,实现http post 并发 异步 demo。以文件为介质保存post参数,执行后,将失败链接的参数回存此文件。HandleCurlMulti 以轮询方式进行,可以设置每次的吞吐量。待优化的地方为文件存储,如果多...
主要介绍了php的curl封装类用法,以实例形式较为详细的讲述了curl封装类及其使用方法,并总结了GET与POST的用法。 使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll,...
主要介绍了php采用curl访问域名返回405 method not allowed提示的解决方法,需要的朋友可以参考下