繁体   English   中英

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

[英]Converting command line cURL request to PHP cURL request (PayPal API)

这是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提供给我的。

我怀疑我的问题出在我的$ data中,我正在传递给"CURLOPT_POSTFIELDS" 格式应该是“NV”(名称=值或键=值),但如果你看看PayPal示例"requestEnvelope.errorLanguage=en_US"这看起来不像一个简单的键=值对,所以我不知道如何格式化请求。

我试过这个:

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

$data = http_build_query($requestAsArray);

但仍未收到PayPal API的回复。 我还尝试将请求作为JSON发送并将$format更改$format “JSON”,但API需要name = value / key = value请求。

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

任何帮助将非常感激!

当您使用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自适应支付电话中使用这种方法,它总是有效。 它也适用于这种情况。

暂无
暂无

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

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