我想重写一个fopen()POST请求与代理一起使用。fopen通过代理POST(支付宝适应性付款)
该代码基于贝宝网站上的示例(https://www.x.com/developers/PayPal/documentation-tools/code-sample/216632)。
对于fopen()以及file_get_contents()和cURL lib,我的默认解决方法不适用于相对复杂的帖子标题和内容。 (要么不经过代理或得到无效的请求:{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 = "[email protected]"; //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().'||';
}
?>
我代理的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));
我代理的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;
我卷曲实验:(笨卷曲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是一个选项吗?
注意:示例代码适用于不在代理之后的代码。
由于提前,
亚历