封装调用接口类

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-22 23:41   11   0
<?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();
//=======================================================================//
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP