封装调用接口类

本文提供了一个 PHP 类的示例,展示了如何使用 CURL 进行 GET 和 POST 请求。通过具体实例介绍了如何抓取不同天气 API 的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php 
header("content-type:text/html;charset=utf-8");
class Collect
{
    public $userAgent = NULL;                // 伪造浏览器标识
    protected $url;                            //网页地址
    protected $object;         
    protected $curlInfo array();             // 采集信息
 
 
    /** 公共初始化 CURL 选项 */
    protected function _init($url='',$issetcookie=FALSE,$isfollow=FALSE)
    {
        $this->url = $url;         
        $this->object = curl_init($this->url);
 
        date_default_timezone_set('PRC');
        curl_setopt($this->object, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->object, CURLOPT_HEADER, 0);
        if(isset($this->userAgent))
        {
            curl_setopt($this->object,CURLOPT_USERAGENT, $this->userAgent); 
        }
        // 是否设置 COOKIE
        if($issetcookie)
        {
            curl_setopt($this->object, CURLOPT_COOKIESESSION, TRUE);    
            curl_setopt($this->object, CURLOPT_COOKIEFILE, "cookiefile");
            curl_setopt($this->object, CURLOPT_COOKIEJAR, "cookiefile");            
        }
        if($isfollow)
        {
            curl_setopt($this->object, CURLOPT_FOLLOWLOCATION,1);    //允许url跳转
        }
         
    }
 
    /**
     * get 抓取网页 调用接口
     * url    抓取的网页
     * pearm  参数
     */
    public function curlGet($url,$pearm='')
    {
        $str '';
        if(is_array($pearm))
        {
            foreach ($pearm as $key => $value) {
                if(empty($str))
                {
                    $str .= '?'.$key.'='.$value;
                }
                else
                {
                    $str .= '&'.$key.'='.$value;
                }
            }
        }
        else
        {
            $str '?'.$pearm;
        }
        $this->_init($url.$str);
        $content  $this->curlExec($this->object);
        // curl_close($this->object);
        return $content;
    }
 
    /**
     * post 抓取网页 调用接口
     * url  网址
     * pearm  参数
     */
    public function curlPost($url,$pearm='')
    {
        $this->_init($url);
         
        curl_setopt($this->object, CURLOPT_POST, 1);        //使用post去传值
        curl_setopt($this->object, CURLOPT_POSTFIELDS, $pearm);  // 传递参数
        curl_setopt($this->object, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded''Content-length: '.strlen($pearm)));
        $content  $this->curlExec($this->object);
        return $content;
    }
    /**
     *  获取抓取信息
     */
    public function getInfo()
    {
        return curl_getinfo($this->object);
    }
    /**
     *  释放句柄
     */
    public function __destruct()
    {
        curl_close($this->object);
    }
    /**
     * 执行curl会话
     */
    protected function curlExec($object)
    {
        $info = curl_exec($this->object);
        if(curl_errno($this->object))
        {
            return  'Curl error: ' . curl_error($this->object);
        }else
        {
            return $info;
        }
    }
 
}
//------------------------------- GET DEMO ----------------------------------------
/* 780天气接口*/
// $url = "http://api.k780.com:88";
// $data = "app=weather.today&weaid=101010100&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml";
// $data = array(
//             'app'   => 'weather.today',
//             'weaid' => '101010100',
//             'appkey'=> '10003',
//             'sign'  => 'b59bc3ef6191eb9f747dd4e83c99f2a4',
//             'format'=> 'xml',
//         );
// $ob = new Collect;
// echo $content = $ob->curlGet($url,$data);
// die();
//=======================================================================//
 
//------------------------------- GET DEMO ----------------------------------------
// 调用百度天气接口(该方式会限制次数)
// $url = "http://api.map.baidu.com/telematics/v3/weather";
// // $data = "location=吉林&output=JSON&ak=FK9mkfdQsloEngodbFl4FeY3";
// $data = array(
//             'location'   => '太原',
//             'output'      => 'JSON',
//             'ak'         => 'FK9mkfdQsloEngodbFl4FeY3',
// );
// $ob = new Collect;
// echo $content = $ob->curlGet($url,$data);
// die();
//=======================================================================//
//------------------------------- POST DEMO ----------------------------------------
// 调用web天气接口 (post)
// $url = 'www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName';
// $param = 'theCityName=石家庄';
// $ob = new Collect;
// $ob->userAgent = $_SERVER['HTTP_USER_AGENT'];
// echo $ob->curlPost($url,$param);
// print_r($ob->getInfo());
// die();
//=======================================================================//
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值