dsrw29618 2014-02-08 09:50
浏览 45
已采纳

如何在'RequestPermissions'操作中通过CURL将HTTP Headers和url变量发送到Paypal?

As the title suggest I am trying to make a CURL call to Paypal, but I have come across an error that I believe has to do with the way I am sending my headers via CURL.

In the code example below I am declaring to two arrays with the values Paypal requires to process the request. $headers will be used in the CURL request as the CURLOPT_HTTPHEADER, and $URLvariable will be concatenated with $url and be used as the CURLOPT_URL.

Code Example:

$headers = array(
    'X-PAYPAL-SECURITY-USERID : MYSSANDBOXUSERID',
    'X-PAYPAL-SECURITY-PASSWORD : MYSSANDBOXPASSWORD',
    'X-PAYPAL-SECURITY-SIGNATURE : MYSSANDBOXUSERSIGNATURE',
    'X-PAYPAL-REQUEST-DATA-FORMAT : JSON',
    'X-PAYPAL-RESPONSE-DATA-FORMAT : JSON',
    'X-PAYPAL-APPLICATION-ID : APP-80W284485P519543T'
);

$URLvariable = array (
        'scope' => 'EXPRESS_CHECKOUT',
        'callback' => 'http://vendorpages.info/success.html', 
        'requestEnvelope' =>  '{"errorLanguage":"en_US"}',
);

foreach ( $URLvariable as $key => $value ) {
    $URLvariable .= '&' . $key . '=' . urlencode ( $value );
}

$url = "https://svcs.sandbox.paypal.com/Permissions/RequestPermissions?" . ltrim ( $URLvariable, '&' );

$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, $url );
curl_setopt( $curl , CURLOPT_HEADER, true);
curl_setopt( $curl , CURLOPT_HTTPHEADER, $headers);
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
$result = curl_exec ( $curl );
curl_close ( $curl );
$result = urldecode ( $result );
$result = explode ( '=',$result );

print_r ($result);

*Please note I have replaced my Paypal sandbox credentials on purpose.

The output of which is:

Array ( [0] => HTTP/1.1 400 Bad Request Server: AkamaiGHost Mime-Version: 1.0 Content-Type: text/html Content-Length: 177 Expires: Sat, 08 Feb 2014 09:47:53 GMT Date: Sat, 08 Feb 2014 09:47:53 GMT Connection: close An error occurred while processing your request.

Reference #166.c60300cc.1391852873.2b18ca34 ) 

Any constructive help is appreciated...

  • 写回答

1条回答 默认 最新

  • 红酒泡绿茶 2014-02-08 11:35
    关注

    One thing I notice is that you've got JSON set in your headers, but you're not passing JSON data. You're just using URL parameters like an NVP request. You need to pass the actual JSON (or XML) request into the CURLOPT_POSTFIELDS option in your CURL.

    What you're doing is causing your endpoint URL to be incorrect, which is why you're getting the error you're getting. The $url that you're passing in CURL should just be:

    https://svcs.sandbox.paypal.com/Permissions/RequestPermissions

    Then, again, the actual request body would go in the POSTFIELDS option. I've always used XML, so here's what my headers look like.

    Array
    (
        [0] => X-PAYPAL-SECURITY-USERID: ****
        [1] => X-PAYPAL-SECURITY-PASSWORD: ****
        [2] => X-PAYPAL-SECURITY-SIGNATURE: ****
        [3] => X-PAYPAL-SECURITY-SUBJECT: 
        [4] => X-PAYPAL-SECURITY-VERSION: 109.0
        [5] => X-PAYPAL-REQUEST-DATA-FORMAT: XML
        [6] => X-PAYPAL-RESPONSE-DATA-FORMAT: XML
        [7] => X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T
        [8] => X-PAYPAL-DEVICE-ID: 
        [9] => X-PAYPAL-DEVICE-IPADDRESS: 192.168.1.115
        [10] => X-PAYPAL-SANDBOX-EMAIL-ADDRESS: developer@email.com
    )
    

    The function that I'm using to process my curl request is:

    function CURLRequest($Request = "", $APIName = "", $APIOperation = "")
        {
            $curl = curl_init();
                    curl_setopt($curl, CURLOPT_VERBOSE, 1);
                    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
                    curl_setopt($curl, CURLOPT_URL, $this -> EndPointURL . $APIName . '/' . $APIOperation);
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $Request);
                    curl_setopt($curl, CURLOPT_HTTPHEADER, $this -> BuildHeaders(false));
    
            if($this -> APIMode == 'Certificate')
            {
                curl_setopt($curl, CURLOPT_SSLCERT, $this -> PathToCertKeyPEM);
            }
    
            $Response = curl_exec($curl);       
            curl_close($curl);
            return $Response;
        }
    

    In that example, $Request was sent as...

    <?xml version="1.0" encoding="utf-8"?>
    <RequestPermissionsRequest xmlns="http://svcs.paypal.com/types/ap">
      <requestEnvelope xmlns="">
        <detailLevel>ReturnAll</detailLevel>
        <errorLanguage>en_US</errorLanguage>
      </requestEnvelope>
      <scope xmlns="">EXPRESS_CHECKOUT</scope>
      <callback xmlns="">http://paypal.angelleye.com/paypal/class/1.4/samples/RequestPermissions-Callback.php</callback>
    </RequestPermissionsRequest>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集