curl cross-domain fetch data

---Restore content begins---

class Curl{
protected $c;
public function __construct(){
$this->c = curl_init();
curl_setopt($this->c,CURLOPT_RETURNTRANSFER,1);
//1 means get the returned content; 0 means output the returned content
curl_setopt($this->c,CURLOPT_HEADER,0);
//0 means do not output the header file, non-0 means output the header file
}
public function getDate($url){
curl_setopt($this->c,CURLOPT_URL,$url);
return $res = curl_exec($this->c);
}
// public function get
public function postData($url,$data){ //data=['mod'=>'product','col_key'=>'product']
$data = $this->createUrlData($data);
curl_setopt($this->c,CURLOPT_HTTPHEADER,array('Expect'));
//To prevent 417 errors, the passed string is larger than 1024 bytes
curl_setopt($this->c,CURLOPT_URL,$url);
curl_setopt($this->c,CURLOPT_POST,1);
//Set the request type to post
curl_setopt($this->c,CURLOPT_POSTFIELDS,$data);
//Add post data to the request
return curl_exec($this->c);
}
public function createUrlData($arr){ //得到mod=product&col_key=product
$str = '';
if(!empty($arr)){
foreach($arr as $key=>$value){
$str.= $key.'='.$value.'&';
}
$str = substr($str,0,-1);
}
return $str;
}
public function __destruct(){
curl_close($this->c);
}
}

---End of recovery content---

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649195&siteId=291194637