gpt4 book ai didi

php - 如何用 PHP 返回 http_code 401

转载 作者:行者123 更新时间:2023-12-02 04:44:39 25 4
gpt4 key购买 nike

我在客户端使用 cUrl。

在我的网络服务 (ws.php) 上,我有这个简单的代码:

<?php
header('HTTP/1.1 401 Unauthorized', true, 401);
echo 'This is an error';
exit;
?>

当我调用这个 ws.php 页面时,我仍然有 http_code = 200。

这是客户端的 cUrl 代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "webservice_url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables); //array()

$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

curl_close($ch);

$code = 200 即使我输入标题代码“401”。

最佳答案

在你的 ws.php 上试试这个:

<?php
ob_start();
header('HTTP/1.0 401 Unauthorized');
echo 'This is an error';
exit;
?>

让我知道这是否有效?

关于php - 如何用 PHP 返回 http_code 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20037188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com