如何在 PHP 中使用带凭据的 Curl

问题描述 投票:0回答:1

尝试使用curl调用api响应。

唯一的文档是这样的: https://stage-sgf-partner-api.azurewebsites.net/api/swagger/ui#/Person/PartnerApi_Person_GetGolferInfoByGolfId

我有用于服务器的客户端 ID 和密码。

这是来自服务器的错误消息。

“无法加载 API 定义。

错误

获取错误 尝试获取资源时出现网络错误。 https://stage-sgf-partner-api.azurewebsites.net/api/swagger.json 获取错误 可能存在跨域 (CORS) 问题吗? URL 来源 (https://stage-sgf-partner-api.azurewebsites.net) 与页面 (https://www.systemtest.the18club.se) 不匹配。检查服务器返回正确的“Access-Control-Allow-*”标头。”

有什么建议可能是错的吗?

$username='xxxxx';
$password='xxxxx';
$URL='https://stage-sgf-partner-api.azurewebsites.net/api/swagger/ui#/Person/PartnerApi_Person_GetGolferInfoByGolfId=114973-122';
    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
    
curl_close ($ch);
php curl
1个回答
0
投票

URL 错误,您复制了文档链接而不是 API 链接,如下所示:

https://stage-sgf-partner-api.azurewebsites.net/api/v1/Persons/GetGolferInfoByGolfId?golfID=1

© www.soinside.com 2019 - 2024. All rights reserved.