与 ASP.NET 的 HttpWebResponse GetResponse 函数等效的 PHP cURL 是什么?

What is the PHP cURL equivalent to ASP.NET's HttpWebResponse GetResponse function?

我正在使用此 not-so-great ASAP Connected 文档。它是为 .NET 编写的,但我正在 PHP 中为客户端创建一个 WordPress 插件。

为了获得授权,它提供了这个片段:

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 凭据放在“授权”header.但是,我得到的回应是空的!它不会 return 任何错误。当我记录 $curl_info 变量时,它输出这个(省略了一些数据):

[01-Jul-2020 21:03:36 UTC] Array
(
    [url] => https://stagingapi.asapconnected.com/api/login
    [content_type] => 
    [http_code] => 401
    [header_size] => 765
    [request_size] => 222
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.183425
    [namelookup_time] => 0.00508
    [connect_time] => 0.006237
    [pretransfer_time] => 0.047479
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => -1
    [starttransfer_time] => 0.183389
    [redirect_time] => 0
    [redirect_url] => 
    [certinfo] => Array
        (
        )

    [http_version] => 2
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 47412
    [connect_time_us] => 6237
    [namelookup_time_us] => 5080
    [pretransfer_time_us] => 47479
    [redirect_time_us] => 0
    [starttransfer_time_us] => 183389
    [total_time_us] => 183425
)

有什么想法吗?

@Barmar 指出,在 the documentation 中,对于 ASAP Connected API,访问令牌位于响应的 headers 中。我只需要添加 curl_setopt($curl, CURLOPT_HEADER, true); 以在响应中包含那些 headers,所以我在 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...
}

再次感谢@Barmar !!

请注意:他们的支持团队似乎并不十分了解他们自己的产品。因此,在询问 API 凭据时,请非常具体。我第一次询问时,他们给了我一个一年多后到期的临时访问令牌。我第二次询问时,他们给了我错误的凭据。第三次是魅力。不幸的是,您必须向他们开票以获得这些凭据,因为它们在管理仪表板中的任何地方都不可用。

如果其他人正在使用 PHP 使用此 API 并有任何疑问,我很乐意提供帮助,因为我也在这段旅程中。当这个项目结束时,我什至可能会写一两个教程。