将命令行cURL请求转换为PHP cURL请求(PayPal API)

时间:2017-01-11 08:25:05

标签: php curl nvp

以下是PayPal对其权限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"。该格式应该是" 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/

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

使用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自适应付款电话中使用此方法,它始终有效。它也适用于这种情况。