247 lines
7.4 KiB
PHP
247 lines
7.4 KiB
PHP
<?php
|
||
/**
|
||
* 通用curl
|
||
* User: bleachin
|
||
*/
|
||
if (!defined('BASEPATH')) exit('No direct script access allowed');
|
||
|
||
class Mycurl
|
||
{
|
||
private $host = '';
|
||
private $user_agent = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36';
|
||
private $cookie = '';
|
||
|
||
private $headers = array();
|
||
private $post_data = '';
|
||
|
||
public function __construct()
|
||
{
|
||
$this->setHeaders();
|
||
}
|
||
|
||
public function setHost($host='')
|
||
{
|
||
$this->host = $host;
|
||
}
|
||
|
||
public function setUserAgent($user_agent='')
|
||
{
|
||
$this->user_agent = $user_agent;
|
||
}
|
||
|
||
public function setCookie($cookie='')
|
||
{
|
||
$this->cookie = $cookie;
|
||
}
|
||
|
||
public function setHeaders($headers='')
|
||
{
|
||
if($headers&&is_array($headers)){
|
||
$_headers = $headers;
|
||
}else{
|
||
$_headers = array();
|
||
$_headers[] = $this->host;
|
||
// $_headers[] = 'Accept-Encoding: gzip, deflate, sdch';
|
||
$_headers[] = $this->user_agent;
|
||
$_headers[] = $this->cookie;
|
||
|
||
$headers && $_headers[] = $headers;
|
||
}
|
||
$this->headers = $_headers;
|
||
}
|
||
|
||
public function httpGet($url='', $data=array())
|
||
{
|
||
$res = false;
|
||
|
||
if (!$url) return $res;
|
||
|
||
if ($data)
|
||
{
|
||
$url = $url.'?'.$this->buildGetQuery($data);
|
||
}
|
||
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_URL, $url);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
//关闭https验证
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||
|
||
if($this->headers)
|
||
{
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
|
||
}
|
||
|
||
$res = curl_exec($ch);
|
||
curl_close($ch);
|
||
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* post 通用方法
|
||
* @param string $url [description]
|
||
* @param [type] $data [description]
|
||
* @param string $type post请求的数据类型
|
||
* @param string $file_path 文件的绝对路径,类似APPPATH.'public/img/'.$photo_path
|
||
* @return [type] [description]
|
||
*/
|
||
public function httpPost($url='', $data, $type='', $file_path='',$time_out = 60)
|
||
{
|
||
$res = false;
|
||
|
||
if (!$url) return $res;
|
||
if ($file_path) $file_path = realpath($file_path);
|
||
|
||
if (!$type || $type=='is_file'|| $type=='xml') {
|
||
$this->post_data = $data;
|
||
}elseif($data && $type=='is_json'){
|
||
$this->post_data = $this->buildJsonQuery($data);
|
||
$this->headers[] = 'Content-Type: application/json; charset=UTF-8';
|
||
}elseif($data && $type=='is_form')
|
||
{
|
||
$this->post_data = http_build_query($data);
|
||
$this->headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
|
||
}elseif($data && $type=='xml'){
|
||
$this->headers[] = 'Content-type: text/xml';
|
||
}
|
||
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_URL, $url);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_POST, true);
|
||
curl_setopt($ch, CURLOPT_ENCODING, ''); // 重要,否则采集数据时会乱码,乱码检测的编码格式为cp936
|
||
//关闭https验证
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||
|
||
if ($file_path && $type=='is_file')
|
||
{
|
||
if (class_exists('\CURLFile'))
|
||
{
|
||
$file_path = new \CURLFile($file_path);
|
||
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // >=5.5.0
|
||
}
|
||
else
|
||
{
|
||
if (defined('CURLOPT_SAFE_UPLOAD')) // 非5.6.0,是5.5.0以下手动设置了true
|
||
{
|
||
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // <=5.5.0
|
||
}
|
||
|
||
$file_path = "@".$file_path; //‘@’符号告诉服务器为上传资源
|
||
}
|
||
}
|
||
|
||
if($this->headers) {
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
|
||
}
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post_data);
|
||
if($time_out){ //超时时间
|
||
// 在尝试连接时等待的秒数
|
||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , $time_out);
|
||
// 最大执行时间
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
|
||
}
|
||
|
||
$res = curl_exec($ch);
|
||
|
||
if(curl_errno( $ch )) {
|
||
$errno = curl_errno( $ch );
|
||
$info = curl_getinfo( $ch );
|
||
print_r($errno);
|
||
print_r($info);
|
||
}
|
||
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
|
||
|
||
curl_close($ch);
|
||
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* put 通用方法
|
||
* @param string $url [description]
|
||
* @param [type] $data [description]
|
||
* @param string $type put请求的数据类型
|
||
* @return [type] [description]
|
||
*/
|
||
public function httpPut($url='', $data, $type='',$time_out = 60){
|
||
$res = false;
|
||
|
||
if (!$url) return $res;
|
||
|
||
if (!$type || $type=='is_file'|| $type=='xml') {
|
||
$this->post_data = $data;
|
||
}elseif($data && $type=='is_json'){
|
||
$this->post_data = $this->buildJsonQuery($data);
|
||
$this->headers[] = 'Content-Type: application/json; charset=UTF-8';
|
||
}elseif($data && $type=='is_form')
|
||
{
|
||
$this->post_data = http_build_query($data);
|
||
$this->headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
|
||
}elseif($data && $type=='xml'){
|
||
$this->headers[] = 'Content-type: text/xml';
|
||
}
|
||
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_URL, $url);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); //定义请求类型
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post_data);
|
||
|
||
if($this->headers) {
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
|
||
}
|
||
if($time_out){ //超时时间
|
||
// 在尝试连接时等待的秒数
|
||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , $time_out);
|
||
// 最大执行时间
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
|
||
}
|
||
|
||
$res = curl_exec($ch);
|
||
|
||
if(curl_errno( $ch )) {
|
||
$errno = curl_errno( $ch );
|
||
$info = curl_getinfo( $ch );
|
||
print_r($errno);
|
||
print_r($info);
|
||
}
|
||
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
|
||
|
||
curl_close($ch);
|
||
return $res;
|
||
}
|
||
private function buildGetQuery($data)
|
||
{
|
||
$querystring = '';
|
||
if (is_array($data)) {
|
||
|
||
foreach ($data as $key => $val) {
|
||
if (is_array($val)) {
|
||
foreach ($val as $val2) {
|
||
$querystring .= urlencode($key).'='.urlencode($val2).'&';
|
||
}
|
||
} else {
|
||
$querystring .= urlencode($key).'='.urlencode($val).'&';
|
||
}
|
||
}
|
||
$querystring = substr($querystring, 0, -1);
|
||
} else {
|
||
$querystring = $data;
|
||
}
|
||
return $querystring;
|
||
}
|
||
|
||
private function buildJsonQuery($data)
|
||
{
|
||
$querystring = '';
|
||
if (is_array($data)) {
|
||
$querystring=json_encode($data);
|
||
}
|
||
return $querystring;
|
||
}
|
||
}
|