最近在处理magento接口的curl,直接贴代码吧
$curl = new Varien_Http_Adapter_Curl();//调用magento的curl
$curl->setConfig(array('timeout' => 1));//配置,可略过
$curl->write(Zend_Http_Client::POST, $url, '1.0','',http_build_query($data));//post
$data = $curl->read();//返回数据
$curl->close();//清除curl
return $data;
核心代码相关,方便参考 lib\Varien\Http\Adapter\Curl.php
/**
* Set the configuration array for the adapter
*
* @param array $config
* @return Varien_Http_Adapter_Curl
*/
public function setConfig($config = array())
{
$this->_config = $config;
return $this;
}
/**
* Send request to the remote server
*
* @param string $method
* @param Zend_Uri_Http $url
* @param string $http_ver
* @param array $headers
* @param string $body
* @return string Request as text
*/
public function write($method, $url, $http_ver = '1.1', $headers = array(), $body = '')
{
if ($url instanceof Zend_Uri_Http) {
$url = $url->getUri();
}
$this->_applyConfig();
// set url to post to
curl_setopt($this->_getResource(), CURLOPT_URL, $url);
curl_setopt($this->_getResource(), CURLOPT_RETURNTRANSFER, true);
if ($method == Zend_Http_Client::POST) {
curl_setopt($this->_getResource(), CURLOPT_POST, true);
curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body);
}
elseif ($method == Zend_Http_Client::GET) {
curl_setopt($this->_getResource(), CURLOPT_HTTPGET, true);
}
if (is_array($headers)) {
curl_setopt($this->_getResource(), CURLOPT_HTTPHEADER, $headers);
}
/**
* @internal Curl options setter have to be re-factored
*/
$header = isset($this->_config['header']) ? $this->_config['header'] : true;
curl_setopt($this->_getResource(), CURLOPT_HEADER, $header);
return $body;
}
/**
* Read response from server
*
* @return string
*/
public function read()
{
$response = curl_exec($this->_getResource());
// Remove 100 and 101 responses headers
while (Zend_Http_Response::extractCode($response) == 100 || Zend_Http_Response::extractCode($response) == 101) {
$response = preg_split('/^\r?$/m', $response, 2);
$response = trim($response[1]);
}
if (stripos($response, "Transfer-Encoding: chunked\r\n")) {
$response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $response);
}
return $response;
}
/**
* Close the connection to the server
*
* @return Varien_Http_Adapter_Curl
*/
public function close()
{
curl_close($this->_getResource());
$this->_resource = null;
return $this;
}
下面贴一个php原生的curl的方法,其实magento封装的和原生的原理都是一样的
/**
* curl request data for carrier
*/
public function curlPost($url, $post_data = array(), $timeout = 10, $header = "", $data_type = "",$param_type = '') {
$header = empty($header) ? '' : $header;
//支持json数据数据提交
if($data_type == 'json'){
$post_string = json_encode($post_data);
}elseif($data_type == 'array') {
$post_string = $post_data;
}elseif(is_array($post_data)){
$post_string = http_build_query($post_data, '', '&');
}
$ch = curl_init(); // 启动一个CURL会话
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
if(strtolower($param_type) == 'post'){
curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
$result = curl_exec($ch);
// 打印请求的header信息
//$a = curl_getinfo($ch);
//var_dump($a);
curl_close($ch);
return $result;
}
你好,不知道从哪可以联系到,我想用magento开发一个面向缅甸的EC购物网站,想找有经验朋友帮忙开发,有兴趣的话请加微信联系(liehuo1234567890)