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

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

與 ASP.NET 的 HttpWebResponse GetResponse 函數等效的

與 ASP.NET 的 HttpWebResponse GetResponse 函數等效的

PHP
www說 2023-07-08 16:44:37
我正在使用這個不太好的 ASAP Connected文檔。它是為 .NET 編寫的,但我正在用它為客戶端創建一個 PHP 插件。為了獲得授權,它提供了以下代碼片段:HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://stagingapi.asapconnected.com/api/login");request.Headers.Add(HttpRequestHeader.Authorization, "user=username&organizationId=id&password=password&apiKey=apikey");request.Accept = "application/json";HttpWebResponse response = (HttpWebResponse)request.GetResponse();string accessToken = response.Headers["asap_accesstoken"];在我的插件中,我嘗試這樣做:$request_url = 'https://stagingapi.asapconnected.com/api/login';$headers = array(? ? //Accept or Content type? ? 'Accept: application/json',? ? //Authorisation? ? 'Authorization: ' . http_build_query(array(? ? ? ? 'user' => ASAPCONNECTED_USERNAME,? ? ? ? 'password' => ASAPCONNECTED_PASSWORD,? ? ? ? 'organizationId' => ASAPCONNECTED_ORGANIZATION_ID,? ? ? ? 'apiKey' => ASAPCONNECTED_API_KEY)));$curl = curl_init($request_url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //Return response as string instead of displaycurl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Ignore any SSL errorscurl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //Specify our headercurl_setopt($curl, CURLOPT_HTTPGET, true); //Specify that this is a GET request$response = curl_exec($curl); //Execute cURL and collect the response$curl_info = curl_getinfo($curl); //Retrieve it's info$curl_error = curl_error($curl); //Retrieve it's errorcurl_close($curl); //Close the cURLif ($curl_error || !$response) {? ? if ($curl_error) {? ? ? ? //An error occurred requesting authorisation from the API!? ? }? ? if (!$response) {? ? ? ? //The response was empty when requesting authorisation from the API!? ? }} else {? ? //The API authorisation request was a success! Do stuff...}所以我認為“GetResponse”就像一個 GET 請求,并且文檔及其片段非常清楚地將 API 憑據放在“Authorization”標頭中。然而,我得到的答復是空的!它不會返回任何錯誤。當我記錄$curl_info變量時,它輸出以下內容(省略了一些數據):
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

在ASAP Connected API 的文檔中,訪問令牌位于響應的標頭中。我只需要添加curl_setopt($curl, CURLOPT_HEADER, true);以在響應中包含這些標頭,因此我的 PHP 請求如下所示:

$request_url = 'https://stagingapi.asapconnected.com/api/login';

$headers = array(

? ? //Accept or Content type

? ? 'Accept: application/json',

? ? //Authorisation

? ? 'Authorization: ' . http_build_query(array(

? ? ? ? 'user' => ASAPCONNECTED_USERNAME,

? ? ? ? 'password' => ASAPCONNECTED_PASSWORD,

? ? ? ? 'organizationId' => ASAPCONNECTED_ORGANIZATION_ID,

? ? ? ? 'apiKey' => ASAPCONNECTED_API_KEY

)));

$curl = curl_init($request_url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //Return response as string instead of display

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Ignore any SSL errors

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //Specify our header

curl_setopt($curl, CURLOPT_HEADER, true);//Include headers in response

curl_setopt($curl, CURLOPT_HTTPGET, true); //Specify that this is a GET request

$response = curl_exec($curl); //Execute cURL and collect the response

$curl_info = curl_getinfo($curl); //Retrieve it's info

$curl_error = curl_error($curl); //Retrieve it's error

curl_close($curl); //Close the cURL

if ($curl_error || !$response) {

? ? if ($curl_error) {

? ? ? ? //An error occurred requesting authorisation from the API!

? ? }

? ? if (!$response) {

? ? ? ? //The response was empty when requesting authorisation from the API!

? ? }

} else {

? ? //The API authorisation request was a success! Do stuff...

}


請注意:他們的支持團隊似乎對他們自己的產品并不是非常了解。因此,在要求提供 API 憑據時,請務必非常具體。我第一次詢問時,他們給了我一個臨時訪問令牌,該令牌將在一年多后到期。我第二次詢問時,他們給了我錯誤的憑據。第三次是一個魅力。不幸的是,您必須向他們開具票證才能獲取這些憑據,因為它們在任何地方的管理儀表板中都不可用。


如果其他人正在使用 PHP 使用此 API 并有任何疑問,我很樂意提供幫助,因為我也在這個旅程中。當這個項目結束時,我什至可能會寫一兩個教程。


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 187 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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