欢迎各位兄弟 发布技术文章
这里的技术是共享的
前提需要php 安装 curl 扩展
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//设置url属性
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);//获取数据
curl_close($ch);//关闭curl
return $output;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
如果你post和get同时使用 那就封装成一个函数来使用吧~
public function curlGet($url,$method,$post_data = 0){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}elseif($method == 'get'){
curl_setopt($ch, CURLOPT_HEADER, 0);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}