Question

I'm trying to rewrite an fopen() POST request to work with a proxy.

The code is based on an example on the Paypal site (https://www.x.com/developers/PayPal/documentation-tools/code-sample/216632).

My default workarounds for fopen() as well as for file_get_contents() and cURL lib don't work for the relatively complex post header and contents. (Either fail to go through proxy or get Invalid request: {0}, even when no proxy is set) Here's the (simplified) post example code:

$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().'||';
 }

?>

My proxy file_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 ) );

My proxied 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;

My cURL experiment: (CodeIgniter curl lib takes care of proxy for me)

$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();

Any ideas where to go from here? I'm a bit stuck..

How do I debug the request going out from cURL? Is the request even reproducible with cURL? Will it work with GET instead of POST? Is XMLRPC an option?

Note: The example code works for me when not behind a proxy.

Thanks in advance,

Alex

Was it helpful?

Solution

Here's the code I used to get it to work with cURL.

Note: It uses the codeigniter cURL lib by Phil Sturgeon.

Note: I resolved to switching from XML to query string for the contents.

    $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();

For those stuck with PHP4 it might be useful answering the original question of getting it to work through proxy with fopen?

Cheers,

Alex

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top