封装下PHP的curl 方便使用
<?php
class qiufeng
{
protected $curl;
protected $header_ = array(
'Content-type: application/x-www-form-urlencoded',
);
protected $Get_info ="https://api.qsub.cn/apis/login";
public function __construct(){
$this->curl = new Curl_Request();
}
public function FunctionName($user,$pwd)
{
array_push($this->header_,"cookie:"."");
$Get_course_list_postdata = array(
'user'=>$user,
'pwd'=>$pwd,
);
$Get_course_list_recontent = $this->curl->curl_post($this->Get_course_list_url,$this->header_,$Get_course_list_postdata);
echo $Get_course_list_recontent;
}
}
class Curl_Request
{
protected $curl;
public function __construct(){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_HEADER, 0);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 1);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->curl, CURLOPT_SSLVERSION,0);
}
public function curl_get($url,$header){
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($this->curl);
if (curl_error($this->curl)) {
return "Error: " . curl_error($this->curl);
} else {
return($data);
}
}
public function curl_post($url,$header,$postdata){
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->Initarr($postdata));
$data = curl_exec($this->curl);
if (curl_error($this->curl)) {
return "Error: " . curl_error($this->curl);
} else {
return ($data);
}
}
protected function Initarr(array $arr){
$temp='';
foreach ($arr as $key => $value) {
$temp =$temp.$key."=".$value.'&';
}
return $temp;
}
function __destruct(){
curl_close($this->curl);
}
}