[英]How do i call an API using php curl correctly? I am getting http status code of 401
I am trying to call an API (using bearer token authorization) in php curl but i am getting a response 401. 我试图在php curl中调用API(使用承载者令牌授权),但是得到响应401。
I have tried using the code from post man but i got a different error with that. 我尝试使用邮递员的代码,但与此同时出现了另一个错误。 I have included the the code. 我已经包含了代码。 The Curl is the first function. 卷边是第一个功能。 I have a different API to get the bearer token and that works fine but when i pass the bearer token to my php Curl request i get http status code of 401 我有一个不同的API获取承载令牌,并且工作正常,但是当我将承载令牌传递给我的php Curl请求时,我得到了401的http状态代码
function restRequestDispute($method, $endpoint, $data, $content_type, $token)
{
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Authorization: Bearer " . $token,
"Content-type: $content_type",
"Accepts: application/json"));
// 'Content-Length: 0'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, '');
$method = strtoupper($method);
switch ($method) {
case "GET":
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
case "POST":
//return $data;
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
default:
throw new Exception("Error: Invalid HTTP method '$method' $endpoint");
return null;
}
$oRet = new StdClass;
$oRet->response = json_decode(curl_exec($ch));
$oRet->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $oRet;
}
function createDispute()
{
try{
$local_date = $date;
$method ="POST";
$content_type="application/json";
$accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiU2hlcmlmZi5PcGVsIiwiZXhwIjoxNTU0MzE2ODAzLCJpc3MiOiJ1cC1uZy5jb20iLCJhdWQiOiJ1cC1uZy5jb20ifQ.8LN6gcoDEpb2i8h9NA0Z_PNxRLweaHh44PN5nbfER10";
$params = array (
"issuerrrn"=> "003409137690",
"transactionamount"=> "-7100",
"localtransactiondate"=> "20181210",
"logcomment"=> "comment",
"amountdispenced"=>"0",
"currentstatus"=> "NotStarted",
"transitionaction"=> "GoodsServicesNotReceived"
);
$endpoint="https://172.**.*.***:***/api/DisputeWIthIssRrn?issuerrrn=003409137690&transactionamount=-7100&localtransactiondate=20181210&logcomment=comment&amountdispenced=0¤tstatus=NotStarted&transitionaction=GoodsServicesNotReceived";
$response = restRequestDispute($method, $endpoint, "", $content_type, $accessToken);
return $response;
} catch(Exception $e){
$returned_result = ["error" => ["status" => "Exception", "data" => $e->getMessage()]];
return $returned_result;
}
}
I had a little tinker with your curl function, moved some things about a little and added in a couple of things - one thing is the enhanced debugging information in the output and the other is the use of cainfo
in the SSL options. 我对您的curl函数进行了一些修改,将一些内容进行了一些cainfo
,并添加了几项内容-一件事是输出中增强的调试信息,另一件事是在SSL选项中使用cainfo
。 Hope it helps 希望能帮助到你
function restRequestDispute($method, $endpoint, $data, $content_type, $token){
try{
/* you can freely download cacert.pem from the internet - always use when performing SSL communications in cURL */
$cacert = '/path/to/your/cacert.pem';
/* add additional debug info to this stream */
$vbh = fopen('php://temp', 'w+');
$method = strtoupper( $method );
$ch = curl_init( $endpoint );
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
"Authorization: Bearer " . $token,
"Content-type: $content_type",
"Accepts: application/json"
)
);
if( parse_url( $endpoint,PHP_URL_SCHEME )=='https' ){
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_CAINFO, $cacert );
}
/* set the options for enhanced debugging */
curl_setopt( $ch, CURLOPT_VERBOSE, true );
curl_setopt( $ch, CURLOPT_NOPROGRESS, true );
curl_setopt( $ch, CURLOPT_STDERR, $vbh );
switch( $method ) {
case "GET":
curl_setopt($ch, CURLOPT_HTTPGET, true );
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE" );
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT" );
break;
case "POST":
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
break;
default:
throw new Exception("Error: Invalid HTTP method '$method' $endpoint");
return null;
}
$oRet = new StdClass;
$oRet->response = json_decode( curl_exec( $ch ) );
$oRet->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$oRet->errors = curl_error( $ch );
/* add verbose information to the output for enhanced debugging */
rewind( $vbh );
$oRet->verbose = stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $ch );
return $oRet;
}catch( Exception $e ){
exit( $e->getMessage() );
}
}
When the function returns if you examine the output of $oRet->verbose
or, as called, $response->verbose
you should see more info which has helped me many times resolve problems with curl requests. 当函数如果检查的输出返回$oRet->verbose
,或如叫, $response->verbose
,你应该可以看到更多信息这帮助了我很多次解决与卷曲的要求的问题。
Looking at how you call the above function I notice this: 看一下如何调用上述函数,我注意到了这一点:
`restRequestDispute( $method, $endpoint, "", $content_type, $accessToken );`
should that not instead be 应该不是
`restRequestDispute( $method, $endpoint, $params, $content_type, $accessToken );`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.