卷曲命令运行平稳,但是将其转换为php并运行使我出现“ 500内部服务错误”

时间:2019-04-12 09:37:40

标签: php curl

curl的新手,正在尝试在php中运行它。但是在php中运行500 internal server error会使我面对面。尝试了许多方法,所以会谦虚地喜欢一些建议。

当前卷曲cmd:

curl -X POST https://www.dbs.com/sandbox/api/sg/v1/oauth/authorize --header "Content-Type:application/json" --header "clientId:clientId3" -d '{ "client_id":"demo", "client_assertion":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJEQlMiLCJpYXQiOjE1NTUwNTg0OTYsImV4cCI6MTU1NTE0NDg5Niwic3ViIjoiSnd0IFNpZ25lZCBUb2tlbiBmb3IgRGVtZSBBcHAgUGFydG5lciBUb2tlbiBmbG93IiwiUEFSVFlfVFlQRSI6MywiQ0xJRU5UX0lEIjoiY2xpZW50SWQzIiwiQ0xJRU5UX1RZUEUiOiJQYXJ0bmVyIiwiQUNDRVNTIjoiQ29tbXVuaXR5IiwiU0NPUEUiOiJSRUFEIiwiYXVkIjoiUGFydG5lcnMiLCJqdGkiOiJTdGFuZGVyZEpXVFRva2VuMSJ9.Pl0YwiAs1UGzIdZXWbnFUGmBDfcKAlkZEcgYd_1NDo2co_6xziut1i0DTwcmcaKDlp7IuOOK1EXYKCmw0qlIu7ciM0DYmdD7M36VbC1TzTx-rVrUJmerRjh_EYY-S4txeCGZtZB0ow6bkgqh6GOz-uwcY7DOg90Da2qVtIk7seQtWi_709UEaNr3XTTorEiIaEqdUN3mmODpLTyrhpzXhop_JIGN1XzNZOfw-vM6DMhL_AiGjDZcvhbo9zPBmbmpECPudfXmtYxK2_4zTVtPDNxH_UUtOVza2MDdIBJCTxY4LQm7dfH-y3grRx6-KyBdUcrchTdqVPS1xATYCGo3QA" }'

当前php代码(转换后):

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.dbs.com/sandbox/api/sg/v1/oauth/authorize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"client_id\":\"demo\", \"client_assertion\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJEQlMiLCJpYXQiOjE1NTUwNTg0OTYsImV4cCI6MTU1NTE0NDg5Niwic3ViIjoiSnd0IFNpZ25lZCBUb2tlbiBmb3IgRGVtZSBBcHAgUGFydG5lciBUb2tlbiBmbG93IiwiUEFSVFlfVFlQRSI6MywiQ0xJRU5UX0lEIjoiY2xpZW50SWQzIiwiQ0xJRU5UX1RZUEUiOiJQYXJ0bmVyIiwiQUNDRVNTIjoiQ29tbXVuaXR5IiwiU0NPUEUiOiJSRUFEIiwiYXVkIjoiUGFydG5lcnMiLCJqdGkiOiJTdGFuZGVyZEpXVFRva2VuMSJ9.Pl0YwiAs1UGzIdZXWbnFUGmBDfcKAlkZEcgYd_1NDo2co_6xziut1i0DTwcmcaKDlp7IuOOK1EXYKCmw0qlIu7ciM0DYmdD7M36VbC1TzTx-rVrUJmerRjh_EYY-S4txeCGZtZB0ow6bkgqh6GOz-uwcY7DOg90Da2qVtIk7seQtWi_709UEaNr3XTTorEiIaEqdUN3mmODpLTyrhpzXhop_JIGN1XzNZOfw-vM6DMhL_AiGjDZcvhbo9zPBmbmpECPudfXmtYxK2_4zTVtPDNxH_UUtOVza2MDdIBJCTxY4LQm7dfH-y3grRx6-KyBdUcrchTdqVPS1xATYCGo3QA\" }");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Clientid: clientId3';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

1 个答案:

答案 0 :(得分:1)

您请求的API似乎需要一个User-Agent标头。

尝试使用带有curl标志的--verbose命令来查看curl正在发送的请求标头。您将得到如下内容:

$ curl --verbose -X POST https://www.dbs.com/sandbox/api/sg/v1/oauth/authorize --header "Content-Type:application/json" --header "clientId:clientId3" -d '{ "client_id":"demo", "client_assertion":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJEQlMiLCJpYXQiOjE1NTUwNTg0OTYsImV4cCI6MTU1NTE0NDg5Niwic3ViIjoiSnd0IFNpZ25lZCBUb2tlbiBmb3IgRGVtZSBBcHAgUGFydG5lciBUb2tlbiBmbG93IiwiUEFSVFlfVFlQRSI6MywiQ0xJRU5UX0lEIjoiY2xpZW50SWQzIiwiQ0xJRU5UX1RZUEUiOiJQYXJ0bmVyIiwiQUNDRVNTIjoiQ29tbXVuaXR5IiwiU0NPUEUiOiJSRUFEIiwiYXVkIjoiUGFydG5lcnMiLCJqdGkiOiJTdGFuZGVyZEpXVFRva2VuMSJ9.Pl0YwiAs1UGzIdZXWbnFUGmBDfcKAlkZEcgYd_1NDo2co_6xziut1i0DTwcmcaKDlp7IuOOK1EXYKCmw0qlIu7ciM0DYmdD7M36VbC1TzTx-rVrUJmerRjh_EYY-S4txeCGZtZB0ow6bkgqh6GOz-uwcY7DOg90Da2qVtIk7seQtWi_709UEaNr3XTTorEiIaEqdUN3mmODpLTyrhpzXhop_JIGN1XzNZOfw-vM6DMhL_AiGjDZcvhbo9zPBmbmpECPudfXmtYxK2_4zTVtPDNxH_UUtOVza2MDdIBJCTxY4LQm7dfH-y3grRx6-KyBdUcrchTdqVPS1xATYCGo3QA" }'

> POST /sandbox/api/sg/v1/oauth/authorize HTTP/1.1
> Host: www.dbs.com
> User-Agent: curl/7.45.0
> ...

因此,然后在您的php代码中添加User-Agent标头(任何值都可以使用):

curl_setopt($ch, CURLOPT_USERAGENT, 'curl/7.45.0');

响应($result)如下:

{
    "access_token" : "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiIDogImh0dHBzOi8vY2FwaS5kYnMuY29tIiwiaWF0IiA6IDE1NTUwNjQ0MTE2MDQsICJleHAiIDogMTU1NTA2ODAxMTYwNCwic3ViIiA6ICJaR1Z0Ync9PSIsInB0eXR5cGUiIDogMywiY2xuaWQiIDogImNsaWVudElkMyIsImNsbnR5cGUiIDogIjIiLCAiYWNjZXNzIiA6ICIxRkEiLCJzY29wZSIgOiAiUkVBRCIgLCJhdWQiIDogImh0dHBzOi8vY2FwaS5kYnMuY29tL2FjY2VzcyIgLCJqdGkiIDogIjU5NjQxOTE1ODUzNzAwMjY5OTMiIH0.gij_4TyE6IX8mziHwK2eZaTXPtDDAnZ3nKAcKJYQJxs",
    "refresh_token" : "G/aZD0gOEoTuWETLSw+vt4OWIs8HFX/50psx2ShSaig=",
    "party_id" : "ZGVtbw==",
    "expire_in" : "1555068011604"
}

HTTP响应代码:200