批改状态:未批改
老师批语:
<?php
$url = "http://apis.juhe.cn/mobile/get";
$data =array (
"phone" => '13805009300',
"key" => "4d6cce531387deeedf359687fb04c163",
);
print_r(juheCurl($url,$data,1));
// 通过curl请求接口
function juheCurl($url, $params = false, $ispost = 0){
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // 默认值,让 cURL 自己判断使用哪个版本。 (强制使用 HTTP/1.1)。
curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData'); // 在HTTP请求中包含一个"User-Agent: "头的字符串。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); // 在尝试连接时等待的秒数。设置为0,则无限等待。
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 要求结果保存到字符串中还是输出到屏幕上
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 爬取重定向页面
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); // Post提交的数据包
curl_setopt($ch, CURLOPT_URL, $url); // 设置URL
} else {
// GET请求,组装url
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch); // 运行cURL,请求URL,把结果复制给变量
if ($response === FALSE) {
echo "cURL Error: " . curl_error($ch); //捕抓异常
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 获取一个cURL连接资源句柄的信息
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号