php 无法打开流:HTTP请求失败! HTTP/1.1 415

dphi5xsq  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(316)

Here is the output

    • 我的密码**
<?php
    if($_SERVER["REQUEST_METHOD"]=="POST"){
        $title = $_POST["title"];
        $type = $_POST["type"];
        $content  = $_POST["content"];
         $description = $_POST['description'];
         $postdata = 
            array(
                'title' => $title,
                'type' => $type,
                'content' => $content,
                'description' => $description,
                
            );
        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'content' => http_build_query($postdata),
            )
        );
        
        $context  = stream_context_create($opts);
        
        $result = file_get_contents('http://localhost:8080/project_war_exploded/admin/blog', false, $context);
        echo $result;

}



?>

我收到错误"文件获取内容(http://localhost:8080/project_war_exploded/admin/blog)":无法打开流:HTTP请求失败! HTTP/1.1 415 "
原因是什么,有人能帮我吗?
就像我说的-从 Postman 那里打电话,效果很好。有什么建议吗?

qxsslcnc

qxsslcnc1#

415表示Unsupported media type。您发送了后端不期望的www-form-urlencoded。将正确的内容类型添加到头中,如本例中的application/json

$opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => "Content-Type: application/json\r\n".
            'content' => http_build_query($postdata),
        )
    );

相关问题