亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 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 關注
  • 122 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號