curl 跨域取数据

---恢复内容开始---

class Curl{
protected $c;
public function __construct(){
$this->c = curl_init();
curl_setopt($this->c,CURLOPT_RETURNTRANSFER,1);
//1表示获取返回的内容;0则输出返回的内容
curl_setopt($this->c,CURLOPT_HEADER,0);
//0表示不输出头文件,非0则输出头文件
}
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'));
//防止417错误,传递的字符串大于1024字节
curl_setopt($this->c,CURLOPT_URL,$url);
curl_setopt($this->c,CURLOPT_POST,1);
//设置请求类型为post
curl_setopt($this->c,CURLOPT_POSTFIELDS,$data);
//添加post数据到请求中
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);
}
}

---恢复内容结束---

猜你喜欢

转载自www.cnblogs.com/ksy-c/p/9004719.html