简体   繁体   English

PHP中的cURL输出

[英]cURL Output in PHP

I have this curl output where I replace with my id, secret and url. 我有这个curl输出,我用我的id,secret和url替换。 It gives correct response. 它给出了正确的答案。

curl -XPOST -H "Cache-Control:no-cache"   -H "Content-Type:application/json"   --user 'id:secret'   'url'

But when I'm trying to write it in php in the following way, it's giving error for invalid cleint. 但是当我试图用以下方式在php中编写它时,它会给无效的cleint带来错误。 How to rewrite curl from the above output in the same way in php? 如何在php中以相同的方式从上面的输出重写curl? Thanks! 谢谢!

$url = 'https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials';
$clientId = 'client_id;
$clientSecret = 'secret_id';    
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD,  $clientId . ":" . $clientSecret);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);

It's giving error - 'invalid cleint', but when I run it in console, it returns access token. 它给出了错误 - '无效的cleint',但是当我在控制台中运行它时,它返回访问令牌。 This code is only for access token, not for payment. 此代码仅用于访问令牌,不用于付款。

Edited: My current code is: 编辑:我目前的代码是:

 $url ='https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials'; $clientId = NETELLER_CLIENT_ID; $clientSecret = NETELLER_CLIENT_SECRET; $curl = curl_init($url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( 'Authorization: Basic '. base64_encode($clientId) . ':' . base64_encode($clientSecret), 'Content-Type:application/json' ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($curl); if (curl_error($curl)) { curl_close($curl); throw new \\Exception('System error. Can not get neteller token'); } curl_close($curl); 

Tried to add the credentials as part of the headers. 试图将凭据添加为标头的一部分。

    $headers = array(
        'Authorization: Basic '. base64_encode($clientID . ':' . $clientSecret),
        'Content-Type:application/json'
    );

 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

Ok I found out. 好的,我发现了。

Update: 更新:

There are 4 reasons why you may get { "error": "invalid_client" } when trying to run your code. 尝试运行代码时,有4个原因导致{ "error": "invalid_client" }

  1. You are using client_id + client_secret from your test account on the production environment or vice versa. 您在生产环境中使用来自测试帐户的client_id + client_secret,反之亦然。
  2. Your client_id or client_secret values are wrong. 您的client_id或client_secret值是错误的。
  3. The IP address from which your application is making outgoing requests is not white-listed in your merchant account. 您的应用程序发出传出请求的IP地址未在您的商家帐户中列入白名单。
  4. You are not sending the Authorization header properly. 您没有正确发送授权标头。
  5. You missed curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Cache-Control:no-cache")); 你错过了curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Cache-Control:no-cache")); from your code. 从你的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM