微信公众号最佳实践 ( 7.6)苹果产品信息查询

苹果产品信息查询

index.php

<?php
define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if($tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
            }
            $this->logger("T ".$result);
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    private function receiveEvent($object)
    {
        $content = "";
        switch ($object->Event)
        {
            case "subscribe":
                $content = "欢迎关注方倍工作室 ";
                break;
            case "unsubscribe":
                $content = "取消关注";
                break;
        }
        $result = $this->transmitText($object, $content);
        return $result;
    }

    private function receiveText($object)
    {
        $keyword = trim($object->Content);
        $category = substr($keyword,0,6);
        $code = trim(substr($keyword,6,strlen($keyword)));
        switch ($category)
        {
            case "苹果":
                include("apple.php");
                $content = getAppleInfo($code);
                break;
            default:
                $content = "";
                break;
        }
        if(is_array($content)){
            $result = $this->transmitNews($object, $content);
        }else{
            $result = $this->transmitText($object, $content);
        }
        return $result;
    }

    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }

    private function transmitNews($object, $arr_item)
    {
        if(!is_array($arr_item))
            return;

        $itemTpl = "    <item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
    </item>
";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

        $newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
        return $result;
    }

    private function logger($log_content)
    {
        if(isset($_SERVER['HTTP_BAE_ENV_APPID'])){   //BAE
            require_once "BaeLog.class.php";
            $logger = BaeLog::getInstance();
            $logger ->logDebug($log_content);
        }else if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
            sae_set_display_errors(false);
            sae_debug($log_content);
            sae_set_display_errors(true);
        }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
            $max_size = 10000;
            $log_filename = "log.xml";
            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
            file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
        }
    }
}


?>

apple.php

<?php
// var_dump(getAppleInfo("358843053134475"));  // 5s
// var_dump(getAppleInfo("358031058974471"));  //5c
// var_dump(getAppleInfo("F17JTV98DTWD"));  //5
// var_dump(getAppleInfo("dmrhc75ldfhw"));  //iPad 2
function getAppleInfo($sn)
{
    $rnd = rand(100,999999);
    $post = "sn=$sn&cn=&locale=&caller=&num=$rnd"; 
    $url = "https://selfsolve.apple.com/wcResults.do";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $result=curl_exec($ch);
    curl_close($ch);

    $result = str_replace("'","",$result);
    preg_match('#setClassAndShow\((.*?)\)#is',          $result, $RegistrationInfo);//注册信息
    preg_match('#displayProductInfo\((.*?)\)#is',       $result, $ProductInfo);     //产品信息
    preg_match('#displayPHSupportInfo\((.*?)\)#is',     $result, $PHSupportInfo);   //电话支持
    preg_match('#displayHWSupportInfo\((.*?)\)#is',     $result, $HWSupportInfo);   //硬件保修

    if (empty($RegistrationInfo)){
        return "很抱歉,此序列号无效。请检查您的信息,然后再试。";
    }

    $registration = explode(",",$RegistrationInfo[1]);
    $product = explode(",",$ProductInfo[1]);
    $phsupport = explode(",",$PHSupportInfo[1]);
    $hwsupport = explode(",",$HWSupportInfo[1]);

    $phsupport_date = "";   //提取电话支持日期
    if (trim($phsupport['0']) == "true"){
        preg_match('#Estimated Expiration Date:(.*?)<br/>#is', $phsupport['2'].$phsupport['3'], $phsupport_date); 
    }
    $hwsupport_date = "";   //提取保修服务日期
    if (trim($hwsupport['0']) == "true"){
        preg_match('#Estimated Expiration Date:(.*?)<br/>#is', $hwsupport['2'].$hwsupport['3'], $hwsupport_date); 
    }

    $title = "苹果产品信息查询";
    $description = "设备名称:".trim($product['1'])."\n".
    ((preg_match("/^\d{8,20}$/",$sn))?"IMEI号":"序列号").":".$product['3']."\n".
    "购买日期:".((trim($registration['2']) == "registration-true")?"已验证":"无效")."\n".
    "电话支持:".((trim($phsupport['0']) == "true")?"有效[".trim($phsupport_date['1'])."]":"已过期")."\n".
    "保修服务:".((trim($hwsupport['0']) == "true")?"有效[".trim($hwsupport_date['1'])."]":"已过期")."\n".
    "\n数据来自苹果公司官方网站";
    $picurl = $product['0'];

    $months = array(
        "01-"=>"January ", "02-"=>"February ", "03-"=>"March ", 
        "04-"=>"April ", "05-"=>"May ", "06-"=>"June ",
        "07-"=>"July ", "08-"=>"August ", "09-"=>"September ",
        "10-"=>"October ", "11-"=>"November ", "12-"=>"December ",
    );
    foreach($months as $key=>$value){
        $description = str_ireplace($value, $key, $description);
    }

    $result = array(); 
    $result[] =  array(
        "Title"=>$title,
        "Description" =>trim($description),
        "PicUrl" =>$picurl,
        "Url" =>""
    );
    return $result;
}


?>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值