CURL:
//post访问网页数据
public
function get_web_content($url, $curl_data)
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "institution", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $curl_data, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1 //
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
// get访问网页数据
public function _curl($url)
{
$ch = curl_init();
$headers = array("Content-type: application/json;charset='utf-8'",
"Accept: application/json",
"Cache-Control: no-cache", "Pragma: no-cache");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); //设置请求方式
// curl_setopt($ch, CURLOPT_POSTFIELDS, $params);//设置提交的字符串
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置头信息
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不进行ssl验证
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
//设置超时时间为1秒,超过1秒则关闭连接
//curl_setopt($ch,CURLOPT_TIMEOUT,1);
//curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
curl_setopt($ch, CURLOPT_HEADER, 0); // 设置是否显示返回头信息 1返回 0不返回
curl_setopt($ch, CURLOPT_NOBODY, 0); //不想在输出中包含body部分,设置这个选项为一个非零值
$result = curl_exec($ch);
curl_close($ch);
return array($result);
}
file_get_contents(get方式,拼接参数)
// 如:QQ校验成功,获取QQ用户信息
$res = file_get_contents("https://graph.qq.com/user/get_user_info?access_token=" . $access_token . "&oauth_consumer_key=" . $app_id . "&openid=" . $openid);
$res = json_decode($res, true); // 解码json并转数组