PHP 发送HTTP请求的几种方式
1、curl仍然是最好的HTTP库,没有之一。 可以解决任何复杂的应用场景中的HTTP 请求
2. 文件流式的HTTP请求比较适合处理简单的HTTP POST/GET请求,但不适用于复杂的HTTP请求
3. PECL_HTTP扩展写代码更加简洁,省事, 但成熟度不好,编程接口不统一,文档和实例匮乏。
1、file_get_contents 发送get请求
<?php
/**
* 发送post请求
* @param string $url 请求地址
* @param array $post_data post键值对数据
* @return string
*/
function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
$post_data = array(
'username' => 'abcdef',
'password' => '123456'
);
send_post('http://xxx.com', $post_data);
2、通过CURL发送get请求
<?php
$ch=curl_init('http://www.xxx.com/xx.html');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
$output=curl_exec($ch);
$fh=fopen("out.html",'w');
fwrite($fh,$output);
fclose($fh);
3、通过fsocket发送get请求
/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('blog.snsgou.com', '/restServer.php', $post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket, "POST $remote_path HTTP/1.0");
fwrite($socket, "User-Agent: Socket Example");
fwrite($socket, "HOST: $remote_server");
fwrite($socket, "Content-type: application/x-www-form-urlencoded");
fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
fwrite($socket, "Accept:*/*");
fwrite($socket, "");
fwrite($socket, "mypost=$post_string");
fwrite($socket, "");
$header = "";
while ($str = trim(fgets($socket, 4096))) {
$header .= $str;
} $data = "";
while (!feof($socket)) {
$data .= fgets($socket, 4096);
} return $data;
}
PHP 发送HTTP请求的几种方式的更多相关文章
- PHP发送HTTP请求的几种方式
转发:https://blog.tanteng.me/2017/07/php-curl-guzzlehttp/ 1)PHP开发中我们常用CURL 方式封装 HTTP请求,什么是CURL? CURL 是 ...
- JavaWeb 发送post请求的2种方式(form、json)
JavaWeb 发送post请求的2种方式(form.json) CreationTime--2018年6月20日10点15分 Author:Marydon 前提:通过HttpClient来实现 ...
- Python使用requests发送post请求的三种方式
1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...
- 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础
1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...
- 使用jmeter发送put请求的三种方式
之前在前公司使用jmeter调试接口时,由于都是get和post请求,所以一直是顺风顺水的,毫无阻拦在短时间内调试完所有接口. 但是呢,在换到新公司后,发现接口请求是多式多样的,get.post必须有 ...
- [转]使用 curl 发送 POST 请求的几种方式
HTTP 的 POST 请求通常是用于提交数据,可以通过这篇文章来了解各种提交方式:四种常见的 POST 提交数据方式.做 Web 后端开发时,不可避免地要自己给自己发请求来调试接口,这里要记录的内容 ...
- .Net Core下发送WebRequest请求的两种方式
1.使用RestSharp.NetCore 2.使用WebApi请求方式
- php发送post请求的4种方式
http://blog.163.com/fan_xy_qingyuan/blog/static/188987748201411943815244/ class Request{ public stat ...
- PHP发送POST请求的三种方式
class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...
随机推荐
- SQL SERVER与C#数据类型对照表
分类 SQL SERVER类型 类型说明 C#类型 精确数字 bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) ...
- java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符
java.sql.SQLSyntaxErrorException: ORA-: 无效字符 at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer. ...
- java代码示例(4—1(作业))
package com.java.union4; import static org.junit.Assert.*; import org.junit.Test; public class Demo ...
- POJ1742Coins(多重背包)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32309 Accepted: 10986 Descripti ...
- 基于vue-cli的改造的多页面开发脚手架
项目的GitHub地址:https://github.com/hellobajie/vue-cli-multipage 该脚手架同时支持vux,scss,less 目录结构 vue-cli-multi ...
- requsets模块和beautifulsoup模块
2.requests模块方法 requests是基于Python开发的HTTP库,使用Requests可以轻而易举的完成浏览器可有的任何操作. request.get() request.post() ...
- luogu P3198 [HNOI2008]遥远的行星
bzoj 洛谷 这题意是不是不太清楚 真正题意:求\[f_i=\sum_{j=1}^{\lfloor i*A \rfloor} \frac{M_i*M_j}{i-j}\] 似乎只能\(O(n*\lfl ...
- [转]Linux/Windows下脚本对拍程序
[新]简单写法 (转载自:https://blog.csdn.net/ylsoi/article/details/79824655) 要求:文件输入输出,且输入输出文件需要对应 Linux: #inc ...
- java中String类型
string对象常用方法 string对象比较方法: string类获取包含子串的方法: 字符串和数字的转换: String类 String对象是不可改变的,字符串一旦创建,内容不能再改变. 构造字符 ...
- latex对齐问题
数学公式居中:可以在公式前后各加两个$$,就可以了 一行对齐:左对齐\leftline{内容} 居中\centerline{内容} 右对齐\rightline{内容} 多行或者段落对齐: 左对齐 \b ...