【问题标题】:fopen POST through proxy (Paypal Adaptive payments)fopen POST 通过代理(Paypal 自适应支付)
【发布时间】:2012-03-13 01:10:21
【问题描述】:

我正在尝试重写 fopen() POST 请求以使用代理。

代码基于 Paypal 网站 (https://www.x.com/developers/PayPal/documentation-tools/code-sample/216632) 上的示例。

我对 fopen() 以及 file_get_contents() 和 cURL lib 的默认解决方法不适用于相对复杂的帖子标题和内容。 (即使设置了 no 代理,也无法通过代理或获取无效请求:{0}) 这是(简化的)帖子示例代码:

$url = trim('https://svcs.sandbox.paypal.com/AdaptiveAccounts/AddBankAccount');
$API_UserName = "sbapi_1287090601_biz_api1.paypal.com"; //TODO
$API_Password = "1287090610"; //TODO
$API_Signature = "ANFgtzcGWolmjcm5vfrf07xVQ6B9AsoDvVryVxEQqezY85hChCfdBMvY"; //TODO
$API_SANDBOX_EMAIL_ADDRESS = "rishaque@paypal.com"; //TODO
$API_DEVICE_IPADDRESS = "127.0.0.1"; //TODO
$API_AppID = "APP-80W284485P519543T";
$API_RequestFormat = "XML";
$API_ResponseFormat = "XML";

$xml = simplexml_load_string($xml_str);
$contents = $xml->asXML();

try
{
    $params = array("http" => array(
                                    "method" => 'POST',
                                    "content" => $contents,
                                    'header' =>  'X-PAYPAL-SECURITY-USERID: ' . $API_UserName ."\r\n" .
                                            'X-PAYPAL-SECURITY-PASSWORD: ' . $API_Password . "\r\n" .
                                            'X-PAYPAL-SECURITY-SIGNATURE: ' . $API_Signature . "\r\n" .
                                            'X-PAYPAL-REQUEST-DATA-FORMAT: ' . $API_RequestFormat . "\r\n" .
                                            'X-PAYPAL-RESPONSE-DATA-FORMAT: ' . $API_ResponseFormat . "\r\n" .
                                            'X-PAYPAL-APPLICATION-ID: ' . $API_AppID . "\r\n" .
                                            'X-PAYPAL-SANDBOX-EMAIL-ADDRESS: ' . $API_SANDBOX_EMAIL_ADDRESS . "\r\n" .
                                            'X-PAYPAL-DEVICE-IPADDRESS: ' . $API_DEVICE_IPADDRESS . "\r\n" ));

     $ctx = stream_context_create($params);

     $fp = @fopen($url, 'r', false, $ctx); // PROXIFY!
     $response = stream_get_contents($fp);
     if ($response === false) {
        throw new Exception("php error message = " . "$php_errormsg");
     }
     fclose($fp);

     // ... handle the response content ...
 }
 catch(Exception $e)
 {
     echo 'Message: ||' .$e->getMessage().'||';
 }

?>

我的代理文件_get_contents():

        if (!empty($_context_params)) {
            $http_arr = $_context_params['http'];
            $http_arr['proxy'] = $proxy_path;
            $http_arr['request_fulluri'] = true;
            $_context_params['http'] = $http_arr;
        }
        else
        {
            $_context_params = array( 'http' => array( 'proxy' => $proxy_path, 'request_fulluri' => true ) );
        }
        return file_get_contents( $_url, false, stream_context_create( $_context_params ) );

我的代理 fopen:

        $parsed_proxy_path = parse_url($proxy_path);
        $_proxy_name = $parsed_proxy_path['host'];
        $_proxy_port = $parsed_proxy_path['port'];
        $proxy_fp = fsockopen( $_proxy_name, $_proxy_port );
        if ( !$proxy_fp )
            return false;
        $parsed_url = parse_url($_url);
        $host = $parsed_url['host'];

        $request = "GET $_url HTTP/1.0\r\nHost:$host\r\n\r\n";

        fputs( $proxy_fp, $request );

        return $proxy_fp;

我的 cURL 实验:(CodeIgniter curl lib 为我处理代理)

$http_params = $params['http'];
$this->curl->create($this->cfg->paypal_req_url);
$this->curl->option('header', true);$this->curl->option('verbose', true);
$this->curl->option('useragent', 'cURL/PHP');

$this->curl->http_header($http_params['header']);
$this->curl->http_header("Content-length: ".strlen($contents));
$this->curl->post(array("content"=> $contents));
$res = $this->curl->execute();

有什么想法可以从这里开始吗?我有点卡住了..

如何调试从 cURL 发出的请求?请求甚至可以用 cURL 重现吗? 它会与 GET 而不是 POST 一起使用吗? XMLRPC 是一种选择吗?

注意:示例代码在不使用代理时适用于我。

提前致谢,

亚历克斯

【问题讨论】:

    标签: php curl paypal fopen paypal-adaptive-payments


    【解决方案1】:

    这是我用来让它与 cURL 一起工作的代码。

    注意:它使用 Phil Sturgeon 的 codeigniter cURL 库。

    注意:我决定从 XML 切换到内容的查询字符串。

        $contents = array(
                            "actionType"=>"PAY",
                            "cancelUrl"=>$cancel_url,
                            "returnUrl"=>$return_url,
                            "currencyCode"=>$currency,
                            "sender"=>$sendingmail,
                            "memo"=>$memo,
                            "requestEnvelope.errorLanguage"=>$error_lang,
                            "receiverList.receiver.amount"=>$amount,
                            "receiverList.receiver.email"=>$receivingmail,
        );
        if (isset($notification_url)) { $contents["ipnNotificationUrl"]=$notification_url; }
        if ($receivingmail == $this->cfg->paypal_email) { $contents["feesPayer"]="SENDER"; }
        if ($sendingmail == $this->cfg->paypal_email) { $contents["senderEmail"]=$sendingmail; }
    
        // ------------------------------
        // create request and add headers
        // ------------------------------
        $options = array( "useragent" => "cURL/PHP" );
        $this->curl->create($this->cfg->paypal_req_url);
        $this->curl->http_header("Content-type", "application/x-www-form-urlencoded");
        $this->curl->http_header("X-PAYPAL-SECURITY-USERID", $this->cfg->paypal_user_name);
        $this->curl->http_header("X-PAYPAL-SECURITY-SIGNATURE", $this->cfg->paypal_signature);
        $this->curl->http_header("X-PAYPAL-SECURITY-PASSWORD", $this->cfg->paypal_password);
        $this->curl->http_header("X-PAYPAL-APPLICATION-ID", $this->cfg->paypal_app_id);
        $this->curl->http_header("X-PAYPAL-REQUEST-DATA-FORMAT", "NV");
        $this->curl->http_header("X-PAYPAL-RESPONSE-DATA-FORMAT", "XML");
        $this->curl->post($contents, $options);
        $response = $this->curl->execute();
    

    对于那些坚持使用 PHP4 的人来说,回答最初的问题可能是有用的,即使用 fopen 通过代理使其工作?

    干杯,

    亚历克斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-19
      • 2021-08-04
      • 2014-12-20
      • 2012-05-05
      • 2014-10-13
      • 2013-11-10
      • 2013-11-07
      • 2014-01-23
      相关资源
      最近更新 更多