为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 PHP 接收和存储 CURL 用户名

如何使用 PHP 接收和存储 CURL 用户名

PHP
萧十郎 2023-10-15 10:47:13
我有命令: 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 和其他网站,但没有找到信息
查看完整描述

1 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

您的正文是 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']



查看完整回答
反对 回复 2023-10-15
  • 1 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号