我有以下 PHP 代碼:<?php$data = array("client_id" => "sipgate-app-web", "grant_type" => "password", "username" => "my_username", "password" => "my_password");$data_string = json_encode($data);$ch = curl_init('https://api.sipgate.com/login/sipgate-apps/protocol/openid-connect/token');curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept: application/json'));$result = curl_exec($ch);echo $result;?>不幸的是,我沒有得到預期的回應。我收到的回復是:{"error":"invalid_request","error_description":"缺少表單參數:grant_type"}當使用像https://onlinecurl.com這樣的在線 cURL 工具和我的 cURL PHP 代碼中相同的數據(URL、標題、數據)時,我得到了正確的響應。這意味著,我的 PHP 代碼有問題。我在 PHP 錯誤日志中沒有收到任何錯誤。手冊說我必須使用以下 cURL 代碼: curl \--request POST \--header 'Content-Type: application/x-www-form-urlencoded' \--header 'Accept: application/json' \--data-urlencode "client_id=sipgate-app-web" \--data-urlencode "grant_type=password" \--data-urlencode "username=my_username" \--data-urlencode "password=my_password" \https://api.sipgate.com/login/sipgate-apps/protocol/openid-connect/token由于我是 cURL 的新手,在谷歌搜索了很多之后,我不知道我做錯了什么。有誰能夠幫助我?編輯:您可以按原樣測試我上面的 PHP 代碼。如果代碼正常工作,您應該得到以下響應:{"error":"invalid_grant","error_description":"無效的用戶憑據"}
沒有得到預期的 PHP cURL 響應
慕尼黑5688855
2021-10-22 15:00:31