与 ASP.NET 的 HttpWebResponse GetResponse 函数等效的

我正在使用这个不太好的 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 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_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...

}

所以我认为“GetResponse”就像一个 GET 请求,并且文档及其片段非常清楚地将 API 凭据放在“Authorization”标头中。然而,我得到的答复是空的!它不会返回任何错误。当我记录$curl_info变量时,它输出以下内容(省略了一些数据):


www说
浏览 228回答 1
1回答

HUWWW

在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 displaycurl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Ignore any SSL errorscurl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //Specify our headercurl_setopt($curl, CURLOPT_HEADER, true);//Include headers in responsecurl_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...}请注意:他们的支持团队似乎对他们自己的产品并不是非常了解。因此,在要求提供 API 凭据时,请务必非常具体。我第一次询问时,他们给了我一个临时访问令牌,该令牌将在一年多后到期。我第二次询问时,他们给了我错误的凭据。第三次是一个魅力。不幸的是,您必须向他们开具票证才能获取这些凭据,因为它们在任何地方的管理仪表板中都不可用。如果其他人正在使用 PHP 使用此 API 并有任何疑问,我很乐意提供帮助,因为我也在这个旅程中。当这个项目结束时,我什至可能会写一两个教程。
打开App,查看更多内容
随时随地看视频慕课网APP