[英]PHP cURL request fails, but works with Postman/curl from Bash
[英]Catch cURL request from bash to PHP
我有幾個 bash 腳本將信息從服務器發送到遠程 PHP 腳本。 我想為這些請求添加某種標記化/授權。
這是 bash 腳本的示例,該腳本通過 cURL 發送 JSON 格式的信息:
echo "${infoArr}" | curl -sS -A "$(hostname)" --request POST "${api_url}" \
-H "Content-Type: application/json" \
-d @-
這是用 PHP 編寫的接收端:
<?php
$data = json_decode(file_get_contents('php://input'), true);
...
我的問題:當我使用 bash 腳本發送信息時,我不知道如何在 PHP 端接收身份驗證。
我可以將字符串添加到 URL,例如https://d.tld/script.php?token= <...>。 但這對我來說似乎很原始。 還有其他方法嗎?
只需在 header 中發送令牌身份驗證,如下所示,
echo "${infoArr}" | curl -sS -A "$(hostname)" --request POST "${api_url}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer __YOUR_AUTH_TOKEN_HERE__"
-d @-
在 PHP 中,您可以使用 function getallheaders()
為你
<?php
$headers = getallheaders();
/*
* Place for authorization methods
*/
$data = json_decode(file_get_contents('php://input'), true);
...
只是var_dump($headers)
看看你收到什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.