布拉猫 2023-02-18 14:56 采纳率: 35.3%
浏览 28
已结题

这段python 如何转成 PHP的请求


user_payload = {
    "header": {
        "authorityType": 5,
        "userName": "3123",
       
    },
    "body": {
        "solutionType": "phone",
        "startDate": "2023-02-18 00:00:00",
        "endDate": "2023-02-18 23:59:59",
     
    }
} 
http_headers = {
    "Accept-Encoding": "gzip, deflate",
    "Content-Type": "application/json",
    "Accept": "application/json"
}
user_payload = json.dumps(user_payload)
response = requests.request("POST", url, data=user_payload, headers=http_headers)
print(response.text)

这段python 如何转成 PHP的请求

  • 写回答

3条回答 默认 最新

  • WZZNB666 2023-02-18 15:02
    关注

    下面是将这段 Python 代码转化为 PHP 请求的示例代码:

    <?php
    
    $url = 'http://example.com'; // 请求的 URL
    $data = array(
        'header' => array(
            'authorityType' => 5,
            'userName' => '3123'
        ),
        'body' => array(
            'solutionType' => 'phone',
            'startDate' => '2023-02-18 00:00:00',
            'endDate' => '2023-02-18 23:59:59'
        )
    );
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/json\r\nAccept: application/json\r\n",
            'method'  => 'POST',
            'content' => json_encode($data),
        ),
    );
    
    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
    echo $response;
    
    ?>
    
    

    在 PHP 中,我们可以使用 file_get_contents() 函数来发送 HTTP 请求。我们可以使用 stream_context_create() 函数创建一个包含请求参数的上下文,然后将其传递给 file_get_contents() 函数。

    在这个例子中,我们使用 json_encode() 函数将请求体转化为 JSON 格式。同时,在请求头中指定了内容类型为 application/json,以及接受的内容类型也是 application/json。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月26日
  • 已采纳回答 2月18日
  • 创建了问题 2月18日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)