ylsunyuan技术论坛
标题:
CRUL类包括GET和POST
[打印本页]
作者:
sun
时间:
2015-4-26 17:40
标题:
CRUL类包括GET和POST
<?php
/**
*
*/
class myCurl
{
function curlGet($url)
{
//$url = "http://apistore.baidu.com/microservice/weather?citypinyin=beijing";
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$result = curl_exec($ch);
//释放curl句柄
curl_close($ch);
//打印获得的数据
return $result;
}
function curlPost($url,$post_data)
{
//$url = "http://localhost/web_services.php";
//$post_data = array ("username" => "bob","key" => "12345");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//post数据
curl_setopt($ch, CURLOPT_POST, 1);
//post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);
curl_close($ch);
//打印获得的数据
return $result;
}
}
$url = "http://apistore.baidu.com/microservice/weather?citypinyin=beijing";
$myCurl = new myCurl();
$res = $myCurl -> curlGet($url);
//echo "<pre>";
//print_r($res);
//echo "</pre>";
$json = json_decode($res,true);
var_dump($json);
echo $json['retData']['city'].$json['retData']['weather']."。气温:".
$json['retData']['temp']."度。风向:".$json['retData']['WD']."风力:".$json['retData']['WS'];
复制代码
欢迎光临 ylsunyuan技术论坛 (http://bbs.ylsunyuan.com/)
Powered by Discuz! X3.2