PHP接口代码

PHP查天气

/**
 * 获取城市天气信息
 * @param string $city 城市拼音  仅支持市级丨省级
 * @return json
 */
public function weather($city)
{
    $xmls = file_get_contents('http://flash.weather.com.cn/wmaps/xml/' . $city . '.xml');
    $xml = simplexml_load_string($xmls);
    $json = json_encode($xml, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    return str_replace("@", "", $json);
} 
虽然只支持填写市级和省级的,但是都是会把所有信息列出来,比如你填写 guangdong 会返回 广州 东莞 深圳 河源 ... 等信息给你,填写 guangzhou 会返回 天河 越秀 白云 等县级 给你!

PHP查快递

/** 
  * 获取物流信息
  * @param string $postid 快递单号 $code 快递识别码,没有默认自动识别
  * @return url
  */ 
public function express($postid,$code)
{
    //接口
    $url = 'https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&nu=' . $postid . '&com=' . $code;

    //创建cookie目录文件
    $cookie = tempnam(sys_get_temp_dir(), 'cookie');;
    $t = parse_url($url);

    //获取cookie并写入
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "$t[scheme]://$t[host]/");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
    curl_exec($curl);

    //使用接口
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
    $data = curl_exec($curl);
    curl_close($curl);
    
    //转换给前端需要的格式并输出
    $odata = json_decode($data, 1);
    $newdata = $odata["data"]["info"]["context"];
    if (empty($odata["data"]["info"]["context"])) {
        return json(['statusCode' => 404, 'msg' => '出现未知错误!']);
    }
    foreach ($newdata as $k => $v) {
        $newdata[$k]['time'] = date("Y-m-d H:i:s", $v['time']);
    }
    $retjson = ['statusCode' => 200, 'msg' => '获取成功', 'data' =>  $newdata];
    return json_encode(retjson,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
通过快递100接口 百度接口 查询物流信息快递信息

PHPQQ空间随机壁纸

<?php

/** 
* QQ空间随机壁纸
* @return url
*/ 

//访问图片配置文件
$url = geturl('https://qzonestyle.gtimg.cn/qzone/qzactStatics/configSystem/data/179/config1.js');
//删除多余字符串
$data = substr($url,'25','-158');
//解析Json
$data = json_decode($data,1)['list'];
//获取图片数量
$num = count($data)-1;
//跳转图片
header("location:".$data[rand(0,$num)]['bg']);

//curl
function geturl($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $content = curl_exec($ch);
    curl_close($ch);
    return ($content);
}

PHP搜狗随机壁纸

<?php
 /**
 * 通过搜狗搜索壁纸接口随机图片
 * @param  string  $ImgClass 图片分类 | $ImgOrder 图片
 */
$GetImgUrl = file_get_contents("https://www.sogou.com/home/data/skinlist?code=".md5('Reaper'.rand(998,19998)));
$GetImgRen = json_decode($GetImgUrl,1);
$ImgClass = $GetImgRen['skinlib'][rand(0,count($GetImgRen['skinlib'])-1)]['skins'];
$ImgOrder = rand(0,count($ImgClass)-1);
header("location:".$ImgClass[$ImgOrder]['bigurl']);
Last modification:July 23rd, 2019 at 03:19 am
If you think my article is useful to you, please feel free to appreciate

One comment

  1. wjwgg

    楼主用的是什么编辑器 typecho自带的吗

Leave a Comment