【问题标题】:What is the PHP cURL equivalent to ASP.NET's HttpWebResponse GetResponse function?与 ASP.NET 的 HttpWebResponse GetResponse 函数等效的 PHP cURL 是什么?
【发布时间】:2020-10-22 09:46:08
【问题描述】:

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

为了获得授权,它提供了这个 sn-p:

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 请求,并且文档和它的 sn-p 非常清楚地将 API 凭据放在“授权”标头中。但是,我得到的响应是空的!它不返回任何错误。当我记录$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
)

有什么想法吗?

【问题讨论】:

  • 访问令牌在响应头中,而不是数据中。您需要使用CURLOPT_HEADER 将其包含在响应中,然后您必须解析返回的标头。我不认为curl 提供任何自动标头解析。
  • 啊哈!好吧,这让我更接近@Barmar!谢谢!所以在添加curl_setopt($curl, CURLOPT_HEADER, true); 之后,它就达到了成功功能。虽然在回复中,它现在告诉我asap_accesstoken: There's no authorization parameters。我没有正确发送标头中的凭据吗?
  • 看起来应该是对的
  • 好的,谢谢@Barmar 让我走到这一步。我想我会再次联系他们的支持团队以获取凭据。他们文档中的句子Note: the username and password is unique to your integration application, and is not related to your regular ASAP application credentials. 让我相信我的客户向我提供了他们的登录凭据,而不是集成应用程序凭据,而且这两个可能相同。

标签: php curl authorization httpwebrequest


【解决方案1】:

@Barmar 指出,在 ASAP Connected API 的 the documentation 中,访问令牌位于响应的标头中。我只需要添加 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...
}

再次感谢@Barmar!

请注意:他们的支持团队似乎对他们自己的产品并不了解。所以在询问 API 凭证时,要非常具体。我第一次问的时候,他们给了我一个临时访问令牌,有效期超过一年。我第二次问,他们给了我不正确的凭据。第三次是魅力。不幸的是,您必须向他们开一张票以获取这些凭据,因为它们在任何地方的管理仪表板中都不可用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多