最近项目负责人要求调接口必须用curl,这不,写一个个把
<?php
/**
* Copyright (c) 2008, ***研发中心
* All rights reserved.
*
* 名 称:
* 摘 要:
* 版 本:1.0
* @author ***
* @since 09.01.08 01:00:14
*/
/**
* class CCurl
*/
class CCurl {
protected
$url="",
$postfields=array();
private
$hd=null,
$options = array(),
$timeout = 5;
/**
* CCurl 类构造函数
* @param
*/
public function __construct($url="") {
$this->initialize($url);
}
public function __destruct(){
curl_close($this->hd);
}
/**
* function 初始化函数
* @ps
* @param $url:str 访问的url
* @return
*/
public function initialize($url)
{
$this->url = $url;
if($url)
$this->hd = curl_init($url);
else
$this->hd = curl_init();
}
/**
* function 设置url
* @ps
* @param
* @return
*/
public function setUrl($url)
{
self::setOption(CURLOPT_URL,$url);
}
/**
* function set option操作
* @ps 原始的封装
* @param $optk:option curl的option
* @param $optv:mixed 设定值
* @return Boolean
*/
public function setOption($optk,$optv)
{
$this->options[$optk] = $optv;
}
/**
* function 上传文件
* @ps
* @param
* @return
*/
public function setFile($fieldname ,$file)
{
$this->postfields[$fieldname] = "@$file";
}
/**
* function post数据
* @ps
* @param
* @return
*/
public function setPost($fieldname ,$post)
{
$this->postfields[$fieldname] = $post;
}
/**
* function get结果
* @ps
* @param
* @return
*/
public function getResult()
{
$this->options[CURLOPT_RETURNTRANSFER] = true;
$this->options[CURLOPT_TIMEOUT] = $this->timeout;
if($this->postfields)
{
self::setOption(CURLOPT_POST,true);
self::setOption(CURLOPT_POSTFIELDS,$this->postfields);
}
curl_setopt_array($this->hd, $this->options);
return curl_exec($this->hd);
}
}
$test = new CCurl("localhost/test.php?dede=asdf");
$test->setUrl("localhost/test.php?dede=1111");
$test->setFile("dll","c:/gdiplus.dll");
$test->setPost("test","asdf");
echo $test->getResult();
?>