【问题标题】:Converting command line cURL request to PHP cURL request (PayPal API)将命令行 cURL 请求转换为 PHP cURL 请求(PayPal API)
【发布时间】:2017-05-25 22:48:38
【问题描述】:

这是 PayPal 对其 Permissions API 的命令行 cURL 请求示例:

curl https://svcs.sandbox.paypal.com/Permissions/GetAccessToken \
-s  \
--insecure \
-H "X-PAYPAL-SECURITY-USERID: api_username" \
-H "X-PAYPAL-SECURITY-PASSWORD: api_password" \
-H "X-PAYPAL-SECURITY-SIGNATURE: api_signature" \
-H "X-PAYPAL-REQUEST-DATA-FORMAT: NV" \
-H "X-PAYPAL-RESPONSE-DATA-FORMAT: NV" \
-H "X-PAYPAL-APPLICATION-ID: app_id" \
-d requestEnvelope.errorLanguage=en_US \
-d token=token \
-d verifier=code

我尝试将上述命令行 cURL 中的凭据、令牌和验证程序输入我的命令行并收到成功的响应。

但是,当我尝试使用我的 PHP 应用程序时,我什至没有收到失败响应。这是我的 php cURL 请求:

class PayPalSession
{
    private $devUsername;
    private $devPassword;
    private $devSignature;
    private $appId;

    public function __construct($devUsername, $devPassword, $devSignature, $applicationId, $format)
    {
        $this->devUsername = $devUsername;
        $this->devPassword = $devPassword;
        $this->devSignature = $devSignature;
        $this->applicationId = $applicationId;
        $this->format = $format;
    }


    /** sendHttpRequest
        Sends a HTTP request to the server for this session
        Input:  $data
        Output: The HTTP Response as a String
    */
    public function sendHttpRequest($data, $endpoint)
    {
        //build eBay headers using variables passed via constructor
        $headers = $this->buildPaypalHeaders();

        //initialise a CURL session
        $connection = curl_init();
        //set the server we are using (could be Sandbox or Production server)
        curl_setopt($connection, CURLOPT_URL, $endpoint);

        //stop CURL from verifying the peer's certificate
        curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);

        //set the headers using the array of headers
        curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);

        //set method as POST
        curl_setopt($connection, CURLOPT_POST, 1);

        //set the Json body of the request
        curl_setopt($connection, CURLOPT_POSTFIELDS, $data);

        //set it to return the transfer as a string from curl_exec
        curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);

        //Send the Request
        $response = curl_exec($connection);

        //print $response;

        //close the connection
        curl_close($connection);

        //return the response
        return $response;
    }



    /** buildPayPalHeaders
        Generates an array of string to be used as the headers for the HTTP request to PayPal
        Output: String Array of Headers applicable for this call
    */
    private function buildPaypalHeaders()
    {
        $headers = array (
            // API credentials for the API Caller account
            'X-PAYPAL-SECURITY-USERID: ' . $this->devUsername,
            'X-PAYPAL-SECURITY-PASSWORD: ' . $this->devPassword,
            'X-PAYPAL-SECURITY-SIGNATURE: ' . $this->devSignature,

            // Application ID
            'X-PAYPAL-APPLICATION-ID: ' . $this->applicationId,

            // Input and output formats
            'X-PAYPAL-REQUEST-DATA-FORMAT : ' . $this->format,
            'X-PAYPAL-RESPONSE-DATA-FORMAT : ' . $this->format,
        );

        return $headers;
    }
}

以下是上面代码中传递的变量:

$format = 'NV';
$endpoint = 'https://svcs.sandbox.paypal.com/Permissions/GetAccessToken'
$data = "requestEnvelope%5BerrorLanguage%5D=en_US&token=$requestToken&verifier=$verificationCode"

..当然 $requestToken$verifierCode 设置为 PayPal 提供给我的内容。

我怀疑我的问题出在我传递给 "CURLOPT_POSTFIELDS" 的 $data 中。格式应该是“NV”(name=value 或 key=value),但是如果您查看 PayPal 示例 "requestEnvelope.errorLanguage=en_US",这看起来不像是简单的 key=value 对,所以我不确定如何为请求。

我试过这个:

$requestAsArray = array('requestEnvelope' => array("errorLanguage" => "en_US"), "token" => "$requestToken", "verifier" => "$verificationCode");

$data = http_build_query($requestAsArray);

但仍然没有收到来自 PayPal API 的响应。我还尝试将请求以 JSON 格式发送并将 $format 更改为“JSON”,但 API 需要 name=value/key=value 请求。

这是 PayPal 对其 API 调用的文档:https://developer.paypal.com/docs/classic/permissions-service/integration-guide/PermissionsUsing/

任何帮助将不胜感激!

【问题讨论】:

    标签: php curl nvp


    【解决方案1】:

    当您使用 NVP 格式的标头时

    'X-PAYPAL-REQUEST-DATA-FORMAT : NV'
    

    请求数组应该是

        $requestAsArray = array(
           'requestEnvelope.errorLanguage' =>"en_US"
           , "token" => "$requestToken"
           , "verifier" => "$verificationCode"
        );
    

    然后使用

    $data = http_build_query($requestAsArray);
    

    当你使用 json 格式的标头时

    'X-PAYPAL-REQUEST-DATA-FORMAT : JSON'
    

    请求数组应该是

    $requestAsArray = array(
       'requestEnvelope' => array("errorLanguage" => "en_US")
       , "token" => "$requestToken"
       , "verifier" => "$verificationCode");
    

    然后使用

    $data = json_encode($requestAsArray);
    

    我在 Paypal 自适应付款调用中使用此方法,它始终有效。在这种情况下它也应该可以工作。

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2014-04-12
      • 2017-08-03
      • 1970-01-01
      • 2016-05-20
      • 2016-08-04
      • 2018-10-21
      • 2017-04-12
      相关资源
      最近更新 更多