广州传奇网络

地址:广州市天河区东圃大马路富华楼C座

电话:13808825895

邮箱:gz020wbs@163.com

QQ:1564443073

网址:http://www.020wbs.com/

首页 > 二次开发Ecshop二次开发 > curl例程与例子

二次开发Ecshop二次开发

二次开发Ecshop二次开发

curl例程与例子

curl是非常有用的库,可以代替用户做一些交互性的事物.

一般流程:

$to_url=$_GET['url'];
print_r($_GET);
if(substr($to_url,0,1)=='/'){
$to_url="http://www.amazon.com".$to_url;
}
echo $to_url;
// 1. 初始化
$ch = curl_init();
// 2. 设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $to_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// 3. 执行并获取HTML文档内容
$output = curl_exec($ch);
$output=preg_replace("#href=\"#","href=\"http://in2.qq-ex.com/amazon.php?url=",$output);
// 4. 释放curl句柄
curl_close($ch);

echo $output;

// 指定代理地址
curl_setopt($ch, CURLOPT_PROXY, '11.11.11.11:8080');
// 如果需要的话,提供用户名和密码
curl_setopt($ch, CURLOPT_PROXYUSERPWD,'user:pass');

 

实例:

<?php
// The XML Request
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml .= "<TranxRequest>\n";
$xml .= "<GatewayID>40001</GatewayID>\n"; // put your Production Gateway ID in the place of 40001
$xml .= "<Products>1.00::1::001::Test Transaction cURL PHP::</Products>\n";
$xml .= "<xxxName>David Smith</xxxName>\n";
$xml .= "<xxxCompany>InternetSecure</xxxCompany>\n";
$xml .= "<xxxAddress>2201 Southdown Rd</xxxAddress>\n";
$xml .= "<xxxCity>Oakville</xxxCity>\n";
$xml .= "<xxxProvince>ON</xxxProvince>\n"; // Can also be xxxState
$xml .= "<xxxPostal>L6L2X9</xxxPostal>\n"; // Can also be xxxZipCode
$xml .= "<xxxCountry>CA</xxxCountry>\n";
$xml .= "<xxxPhone>905-555-1221</xxxPhone>\n";
$xml .= "<xxxEmail>service@internetsecure.com</xxxEmail>\n";
$xml .= "<xxxCard_Number>4715**********40</xxxCard_Number>\n"; // put in a credit Card Number
$xml .= "<xxxCCMonth>01</xxxCCMonth>\n"; // put in valid Expiry Month must be 2 digits
$xml .= "<xxxCCYear>2015</xxxCCYear>\n"; // put in valid Expiry Year must be 4 digits
$xml .= "<CVV2>876</CVV2>\n";
$xml .= "<CVV2Indicator>1</CVV2Indicator>\n";
$xml .= "<xxxTransType>00</xxxTransType>\n";
$xml .= "</TranxRequest>\n";

// Post URL
$url = "https://secure.internetsecure.com/process.cgi"; // Production Post URL

// Pass Request Mode X with the XML Request URL encoded
$postData = "xxxRequestMode=X&xxxRequestData=".URLEncode($xml);

// Get the curl session object
$session = curl_init();

// Pass the Content-Type=application/x-www-form-urlencoded
$header[] = "Content-Length: ".strlen($postData);
$header[] = "Content-Type: application/x-www-form-urlencoded";

// Set the POST options.
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session,CURLOPT_URL,$url);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postData);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, $header);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Close the session and Print to screen transaction Data
$response = curl_exec($session);
curl_close($session);

echo "<pre><em><strong>Transcation Request URL encoded:</strong></em><br \>";
echo str_replace('>','&gt;',str_replace('<','&lt;',str_replace('><',"&gt;\n&lt;",$postData))).'</pre><br \>';

echo "<pre><em><strong>Transcation Request XML:</strong></em><br \>";
echo "<strong>Receipt Number : </strong>". $TRX ."<br \>"; // use this line to display the Receipt Number
echo "<strong>GUID: </strong>". $GUID ."<br \>"; // use this line to display the GUID
echo str_replace('>','&gt;',str_replace('<','&lt;',str_replace('><',"&gt;\n&lt;",$xml))).'</pre><br \>'; // use this line to display the XML only

echo "<pre><em><strong>Transcation Response:</strong></em><br \>";
echo str_replace('>','&gt;',str_replace('<','&lt;',str_replace('><',"&gt;\n&lt;",$response))).'</pre><br \>';
?>