php - 如何使用 PHP 接收和存储 CURL 用户名
问题描述
我有命令:
curl -k -u **username:password** --request POST -H "Content-Type:application/json" -d "{\"name\":\"John\",\"age\":\"31\",\"test\":\"New York\"}" http://siteteste.com/receive.php
我想用 PHP 获取服务器端的用户名和密码。我正在使用 POST 方法。
我的PHP:
<?php
// Only allow POST requests
***// I want the username and password here with PHP POST.***
***$user = $_POST[""]; // Not working :(***
if (strtoupper($_SERVER['REQUEST_METHOD']) != 'POST') {
throw new Exception('Only POST requests are allowed');
}
// Make sure Content-Type is application/json
$content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
if (stripos($content_type, 'application/json') === false) {
throw new Exception('Content-Type must be application/json');
}
// Read the input stream
$body = file_get_contents("php://input");
// Decode the JSON object
$object = json_decode($body, true);
// Throw an exception if decoding failed
if (!is_array($object)) {
throw new Exception('Failed to decode JSON object');
}
// Display the object
$getit = $object['test'];
print $getit;
我该怎么做?
我尝试,但不工作。
我搜索 StackOverflow 和其他网站,但没有找到信息
解决方案
你的身体是 json 编码的。
您将在此处获取数据:
$body = file_get_contents("php://input");
// Decode the JSON object
$object = json_decode($body, true);
var_dump($object); // <-- do this to view your data.
您使用的-u
选项用于身份验证。例如http-authentication。
如果要添加数据,则必须修改-d
参数。例如,我在这里添加了一个用户:
-d "{\"name\":\"John\",\"age\":\"31\",\"test\":\"New York\",\"user\":\"test-user\"}"
或者在另一种情况下,您真的想要http-authentication -data 使用:
$_SERVER['PHP_AUTH_USER']
$_SERVER['PHP_AUTH_PW']
推荐阅读
- angular - formBuilder 控件的 setValue() 和 patchValue() 方法在 cordova 插件的方法调用中不起作用
- unity3d - 在 Unity3d 中切换渲染器的纹理
- vue.js - vuejs 和 vuetify(CRUD 操作)
- r - 将 Sys.time() 转换为字符而不丢失时区?
- java - Python客户端不接收来自Spring websocket服务器的消息
- python - 如何强制 python 可重现?
- c++ - 使用正则表达式在 C++ 中排除子文件夹
- javascript - 传单 JS Cluster Maker 捕获 ids 孩子
- python - Color network graph node lines on click in Bokeh, Python
- r - 用函数创建矩阵