作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在客户端使用 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/
我是一名优秀的程序员,十分优秀!