|
如今各种WEB应用层出不穷,作为一名程序员,怎能没有一技防身?《PHP实现手机归属地查询视频教程》会带大家从0开始开发一个完整的WEB应用,从框架结构到流程分析再到数据缓存,相信你学习完本课之后收获到的不仅仅是学会了开发一个应用。 课程播放地址: 该老师讲课风格: 教师讲课生动形象,机智诙谐,妙语连珠,动人心弦。一个生动形象的比喻,犹如画龙点睛,给学生开启智慧之门;一种恰如其分的幽默,引来学生会心的微笑,如饮一杯甘醇的美酒,给人以回味和留恋;哲人的警句、文化的箴言不时穿插于讲述中间,给人以思考和警醒。 本视频中较为难点是API请求数据了: 方法一(若为post方式,只适用于一维数组) /**
* curl发送htpp请求
* 可以发送https,http,get方式,post方式,post数据发送
*/
public function dataRequest($url,$https=true,$method='get',$data=null)
{
//初始化curl
$ch = curl_init($url);
//字符串不直接输出,进行一个变量的存储
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//https请求
if ($https === true) {
//确保https请求能够请求成功
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
}
//post请求
if ($method == 'post') {
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
//发送请求
$str = curl_exec($ch);
$aStatus = curl_getinfo($ch);
//关闭连接
curl_close($ch);
if(intval($aStatus["http_code"])==200){
return json_decode($str);
}else{
return false;
}
}方法二(若为post方式,适用于二维数组) /**
* @Purpose : curl发送htpp请求,可以发送https,http,get方式,post方式,post数据发送
* @Author : Chrdai
* @Method Name : SendDataByCurl()
* @parameter : string $url 传送的 url
* boolean $https 是否使用 https
* string $method 传递方法
* array $data 数据
* @return : 成功返回对方返回的结果,是非返回 false
*/
function SendDataByCurl($url,$https=true,$method='get',$data=null)
{
// 初始化curl
$ch = curl_init($url);
// 字符串不直接输出,进行一个变量的存储
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// https请求
if ($https === true) {
// 确保https请求能够请求成功
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
}
// post请求
if ($method == 'post') {
curl_setopt($ch,CURLOPT_POST,true);
// 所需传的数组用http_bulid_query()函数处理一下,就可以传递二维数组了
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
// 发送请求
$str = curl_exec($ch);
$aStatus = curl_getinfo($ch);
// 关闭连接
curl_close($ch);
if(intval($aStatus["http_code"])==200){
return json_decode($str);
}else{
return false;
}
}方法三(若为post方式,适用于传递 json) /**
* @Purpose : curl发送htpp请求,可以发送https,http,get方式,post方式,post数据发送
* @Author : Chrdai
* @Method Name : SendDataByCurl()
* @parameter : string $url 传送的 url
* boolean $https 是否使用 https
* string $method 传递方法
* array $jsonStr 需要传递的 json 字符串
* @return : 成功返回对方返回的结果,是非返回 false
*/
function SendDataByCurl($url,$https=true,$method='get',$jsonStr=null)
{
// 初始化curl
$ch = curl_init($url);
// 字符串不直接输出,进行一个变量的存储
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// https请求
if ($https === true) {
// 确保https请求能够请求成功
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
}
// post请求
if ($method == 'post') {
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
// 只需要用个 http 头就能传递 json 啦!
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($jsonStr)
)
);
}
// 发送请求
$str = curl_exec($ch);
$aStatus = curl_getinfo($ch);
// 关闭连接
curl_close($ch);
if(intval($aStatus["http_code"])==200){
return json_decode($str);
}else{
return false;
}
} |