php 模拟http发送请求三种方式(curl,stream流的方式,socket)

895次阅读
没有评论

一,curl

cURL 是一个用来传输数据的工具,支持多种协议,如在 Linux 下用 curl 命令行可以发送各种 HTTP 请求。PHP 的 cURL 是一个底层的库,它能根据不同协议跟各种服务器通讯,HTTP 协议是其中一种。

post请求

  • public static function http_post($url,$data_string,$timeout = 60)
  • {
  • //curl验证成功
  • $ch = curl_init($url);
  • curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  • curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  • curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 跳过证书检查
  • curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  • curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  • 'Content-Type: application/json',
  • 'Content-Length: ' . strlen($data_string)
  • ));
  •  
  • $result = curl_exec($ch);
  • if (curl_errno($ch)) {
  • print curl_error($ch);
  • }
  • curl_close($ch);
  • return $result;
  • }

GET请求 public function doCurlGetRequest($url,$data,$timeout = 50){ if($url == “” || $timeout <= 0){ return false; } $url = $url.’?’.http_build_query($data); $con = curl_init((string)$url); curl_setopt($con, CURLOPT_HEADER, 0); curl_setopt($con, CURLOPT_RETURNTRANSFER,1); curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout); $output = curl_exec($con); //释放curl句柄 curl_close($con); return $output; }

1,curl请求http

$url = ‘//www.jb51.net’;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
var_dump($data);

2,curl请求https

$url = ‘https://www.jb51.net’;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
$data = curl_exec($curl);
curl_close($curl);
var_dump($data);

/**当请求https的数据时,会要求证书,这时候,加上下面这两个参数,规避ssl的证书检查

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts

**/

二,stream流的方式

stream_context_create 作用:创建并返回一个文本数据流并应用各种选项,可用于 fopen(), file_get_contents() 等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

**详细介绍地址www.jb51.net/article/68891.htm

post 例子

function post($url$data)

{

  $postdata = http_build_query(

    $data

  );//数组转化为路径格式

  $opts array('http' =>

           array(

             'method' => 'POST',

             'header' => 'Content-type: application/x-www-form-urlencoded',

             'content' => $postdata

           )

  );

  $context = stream_context_create($opts);

  $result file_get_contents($url, false, $context);

  return $result;

}

三,socket方式

使用套接字建立连接,拼接 HTTP 报文发送数据进行 HTTP 请求。

一个 GET 方式的例子:

<?php

$fp fsockopen("www.example.com", 80, $errno$errstr, 30);

if (!$fp) {

  echo "$errstr ($errno)<br />\n";

else {

  $out "GET / HTTP/1.1\r\n";

  $out .= "Host: www.example.com\r\n";

  $out .= "Connection: Close\r\n\r\n";

  fwrite($fp$out);

  while (!feof($fp)) {

    echo fgets($fp, 128);

  }

  fclose($fp);

}

?>

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 11 月
 12345
6789101112
13141516171819
20212223242526
27282930  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别 量子位的朋友们 2025-1...