PHP 发送HTTP 请求

本文介绍如何使用PHP发送POST请求和GET请求,并展示了如何处理接收到的POST数据。通过示例代码,读者可以了解到如何构造HTTP请求及解析响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Post请求

(1) 发送POST请求

<?php
function send_post($url,$post_data=''){
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($post_data)
    )
);
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    print_r($result);
}
$url='http://127.0.0.1/xx_platform/sync_client/sync.php';
send_post($url,$post_data=array('op'=>'rose99','action'=>'13'));

接受Post请求

<?php
// 定义主方法
// function mainLogic(){
//     print_r("fsdaf");
// }


// 接收post过来的请求
$data = $_POST['op'];
$data1 = $_POST['action'];
$ret_array = ['ret' => 0, 'msg' => $data,'actn'=>$data1];
print_r($ret_array);

//  echo file_get_contents("php://input")  //获取所有传过来的值
//  print_r($_POST); // 获取所有post传过来的值

Get请求

(2) 发送Get请求

<?php
$url='http://www.baidu.com/';
$html = file_get_contents($url);
echo $html;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叱咤少帅(少帅)

如果文章对你有帮助就打赏下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值