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&amp;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请求的几种方式的更多相关文章

  1. PHP发送HTTP请求的几种方式

    转发:https://blog.tanteng.me/2017/07/php-curl-guzzlehttp/ 1)PHP开发中我们常用CURL 方式封装 HTTP请求,什么是CURL? CURL 是 ...

  2. JavaWeb 发送post请求的2种方式(form、json)

      JavaWeb 发送post请求的2种方式(form.json) CreationTime--2018年6月20日10点15分 Author:Marydon 前提:通过HttpClient来实现 ...

  3. Python使用requests发送post请求的三种方式

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  4. 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础

    1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...

  5. 使用jmeter发送put请求的三种方式

    之前在前公司使用jmeter调试接口时,由于都是get和post请求,所以一直是顺风顺水的,毫无阻拦在短时间内调试完所有接口. 但是呢,在换到新公司后,发现接口请求是多式多样的,get.post必须有 ...

  6. [转]使用 curl 发送 POST 请求的几种方式

    HTTP 的 POST 请求通常是用于提交数据,可以通过这篇文章来了解各种提交方式:四种常见的 POST 提交数据方式.做 Web 后端开发时,不可避免地要自己给自己发请求来调试接口,这里要记录的内容 ...

  7. .Net Core下发送WebRequest请求的两种方式

    1.使用RestSharp.NetCore 2.使用WebApi请求方式

  8. php发送post请求的4种方式

    http://blog.163.com/fan_xy_qingyuan/blog/static/188987748201411943815244/ class Request{ public stat ...

  9. PHP发送POST请求的三种方式

    class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...

随机推荐

  1. @RequestBody

    之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...

  2. Linux command ------ vi / vim

    EDIT mode to GENERAL mode: press ESC General mode: operate file :q!    :force to close the file but ...

  3. keepalive的工作原理和如何做到健康检查

    keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议. 虚拟路由冗余协议,可以认为是实现路由器高可用的 ...

  4. Python基础【day01】:Hello World程序(二)

    本节内容 安装 Hello World程序 变量 一.Python安装 windows 1 2 3 4 5 6 7 1.下载安装包     https://www.python.org/downloa ...

  5. JavaEE学习总结(十三)—JavaWeb、JSP、Servlet与DVD管理系统

    一.JSP基础知识 1.0.创建数据库与表 /* Navicat MySQL Data Transfer Source Server : 127.0.0.1 Source Server Version ...

  6. JavaEE学习总结(十二)—MyEclipse开发工具与HTML

    一.MyEclipse MyEclipse,是在eclipse 基础上加上自己的插件开发而成的功能强大的企业级集成开发环境,主要用于Java.Java EE以及移动应用的开发.MyEclipse的功能 ...

  7. MySQL数据库中.SQL文件的导出方式

    转自:http://tech.watchstor.com/management-117401.htm 在MySQL数据库中导入SQL文件是件很麻烦的事情,但是这是一项大家非常值得学习的技术,本文就从最 ...

  8. 《深入理解java虚拟机》第三章 垃圾收集器与内存分配策略

    第三章 垃圾收集器与内存分配策略 3.1 概述 哪些内存需要回收 何时回收 如何回收 程序计数器.虚拟机栈.本地方法栈3个区域随线程而生灭. java堆和方法区的内存需要回收.   3.2 对象已死吗 ...

  9. dos 设置 Windows 网络命令

    dos 设置Windows 命令: netsh interface ip set address name="本地连接" source=static addr=172.16.12. ...

  10. 表格控件QTableWidget

    搭配QTableWidgetItem使用 样式: import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QApplica ...