为了账号安全,请及时绑定邮箱和手机立即绑定

与 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 关注
  • 227 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号