最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP 使用 POST 方式向https发送数据请求
时间:2016-05-24 编辑:简简单单 来源:一聚教程网
例子
代码如下 | 复制代码 |
$data = '{"type":"news", "offset":50, "count":20 }'; // 发送数据 // 输出 ';'; |
例子
方法1感觉也很笨拙,似乎也很难满足我的需求,最后忘记在哪个英文网站上找到了下面这个方法:
代码如下 | 复制代码 |
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; } |
试用了一下,感觉效果非常好,简洁,通用,而且返回的内容仅仅是Body中内容
-
下一个: PHP7新功能使用教程详解
相关文章
- php外部发送大量数据包解决方法 01-15
- phpddos攻击的解决办法(服务器发外网发送大量数据包) 12-06
- PHP利用socket模拟post之fsockopen发送数据 10-27
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31