快乐每一天

自己选择的路,跪着也要走下去...

PHP7.0.7微信公众号开发-新增临时素材(解决 {"errcode":41005,"errmsg":"media data missing hint: )

微信官文文档: 注意事项
1、对于临时素材,每个素材(media_id)会在开发者上传或粉丝发送到微信服务器3天后自动删除(所以用户发送给开发者的素材,若开发者需要,应尽快下载到本地),以节省服务器资源。

2、media_id是可复用的。

3、素材的格式大小等要求与公众平台官网一致。具体是,图片大小不超过2M,支持png/jpeg/jpg/gif格式,语音大小不超过5M,长度不超过60秒,支持mp3/amr格式

4、需使用https调用本接口。调用示例(使用curl命令,用FORM表单方式上传一个多媒体文件):curl -F media=@test.jpg "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"

根据微信的官方文档开看,我们只需要传三个参数 access_token、type、@fileurl(绝对路径)

所以笔者第一次就按照这个文档来了,传了三个参数,但是上传返回 48001 好吧接口未授权,然后笔者去微信官方申请了一个测试号,录入AppID和AppSecret 然后继续重来,这次没出现 48001 出现了一个 45001 重点来了,45001 看后面的英文意思就是媒体文件不存在,怎么可能不存在呢?返回代码打印了文件路径,发现路径没有任何问题,继续重来还是45001。郁闷了,然后用命令进行测试看是否是问下的接口问题,curl -F media=@test.jpg "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE" 这行命令代码换上正确的参数后 执行 发现能正确返回media_id.

2019-06-16T14:52:45.png

那么到底是什么问题呢?现在我们能确定两点1:不是微信接口的问题,2:不是路径问题,让我不禁怀疑到curl函数的问题,刚开始没怀疑它是因为这个函数能正确的获取的access_token但是现在不得不怀疑是否上传文件的时候要发生改变。好吧,咱们去百度curl模拟上传文件发现了一个很重要的问题

PHP5.5以下是支持@+文件这种方式上传文件

PHP5.5以上是支持 new CURLFile(文件) 这种方式上传文件

网上所有的案例百分之就是都是 路径加了 @ ,这就是一个坑。

然后笔者的却是php7.0.7那么事不宜迟赶紧换方式上传试试,改掉文件上传的方式后,测试成功

真是皇天不负有心人啊,说了这么多不如给大家分享点代码,那么下面上代码吧

2019-06-16T14:54:29.png
2019-06-16T14:54:42.png

总结:总之微信的文档不会写的很详细,这就需要我们一步步排查错误,大胆怀疑不能错的地方。然后谷歌、百度看看大牛们都怎么处理的。祝你们能写出一个完美的程序!

Basewx.php

<?php
namespace app\index\controller;
use think\Db;
class Basewx
{

    protected $appid;
    protected $appsecret;
    protected $templateId;
    protected $token = null;
    public $data = null;
    public function __construct($appid, $appsecret)
    {
        $this->appid = $appid;
        $this->appsecret = $appsecret;
        $this->token = $this->getToken();
    }

    // 获取TOKEN,一天就2000条,用一次少一次,跟获取用户信息的access_token 不一样。
    public static function getToken(){
        $tokeninfo=Db::name('access_token')->find(1);
        $time=time()-$tokeninfo['time'];
        if($time>1800){
            $appid = config('appid');
            $secret = config('secret');
            $urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$appid . "&secret=" . $secret;
            $outputa = self::curlGet($urla);
            $result = json_decode($outputa, true);

            Db::name('access_token')->where('id',1)->update(array('token'=>$result['access_token'],'time'=>time()));

            return $result['access_token'];
        }else{
            return $tokeninfo['token'];
        }
    }


    public function menuCreate($data)
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$this->getToken();
        $menu = array();
        $i=0;
        foreach ($data as $item){
            if($i==0){
                $menu['button'][$i]['name'] = $item['name'][0];
                $menu['button'][$i]['type'] = 'click';
                $menu['button'][$i]['key'] = $item['name'][1];
            }else{
                $menu['button'][$i]['name'] = $item['name'][0];
                $menu['button'][$i]['type'] = 'view';
                $menu['button'][$i]['url'] = $item['name'][1];
            }

            $i++;
        }

        $data = self::xjson_encode($menu);
        $data = str_replace('\/','/',$data);
        $result = self::curlPost($url,$data);
        return json_decode($result,true);
    }


    public static function curlGet($url = '', $options = array())
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    public static function curlPost($url = '', $postData = '', $options = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        if($data === false)
        {
            echo 'Curl error: ' . curl_error($ch);exit();
        }
        curl_close($ch);
        return $data;
    }

    //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
    protected function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /*
    * PART 素材管理
    */
    //新增临时素材(原多媒体文件)( (图片(image)、语音(voice)、视频(video)和缩略图(thumb))) ,临时素材 不包括图文消息
    // $weixin->upload_temporary_material("thumb", "logo.jpg"); //logo.jpg须放于类同目录,注意路径
    // { ["type"]=> string(5) "thumb" ["thumb_media_id"]=> string(64) "4rqefcVf-dcsnmuz1Q9ESn_KC3sBVuZhC7JM5-0fEvrttwQFs2KNErCw2YTYPx_l" ["created_at"]=> int(1463105848) }
    public function upload_temporary_material()
    {

        $file='./tmp.png';
//        $newpath='@'.$_SERVER['DOCUMENT_ROOT'].$path;
//        $data = array("media"  => $newpath);

        if (PHP_OS == "Linux"){        //Linux
            $data = array("media"  => new \CURLFile($file));
        }else{                        //WINNT
            $data = array("media"  => "@".dirname(__FILE__).'\\'.$file);
        }
//        $data = array("media"  => "/tmp.png");

        var_dump($data);

        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->getToken()."&type=image";
        $res = $this->http_request($url, $data);
        return json_decode($res, true);
    }

    public static function xjson_encode($data)
    {
        if(version_compare(PHP_VERSION,'5.4.0','<')){
            $str = json_encode($data);
            $str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
                return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
            },$str);
            return $str;
        }
        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }
}

Index.php

<?php
namespace app\index\controller;
use think\Db;

class Index
{


    public function index(){
        header('Content-type:text');
        $token="weixin";
        $post=request()->param();
        if (!isset($post['echostr'])) {
            $this->responseMsg();
        }else{
            $this->valid($token);
        }
    }

    public function test(){
        $class = new \app\index\controller\Basewx(config('appid'),config('secret'));
        $result = $class->upload_temporary_material();
        var_dump($result);
    }

    /*创建菜单*/
    /*http://m.cssnb.com/index/index/createmenu*/
    public function createmenu(){
        $class = new \app\index\controller\Basewx(config('appid'),config('secret'));

        $data[0]['name'] = array('客服','kefu98');
        $data[1]['name'] = array('官网','http://www.baidu.com');
        $result = $class->menuCreate($data);
        if($result['errcode']==0){
            echo '<h1>创建菜单成功!</h1>';
        }else{
            echo '<h1>创建菜单失败:'.$result['errmsg'].'</h1>';
        }
    }

    //验证签名
    public function valid($token)
    {
        $post=request()->param();
        $echoStr = $post["echostr"];
        $signature = $post["signature"];
        $timestamp = $post["timestamp"];
        $nonce = $post["nonce"];
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature){
            echo $echoStr;
            exit;
        }
    }

    //响应消息
    public function responseMsg()
    {
//        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        $postStr = file_get_contents('php://input');
        if (!empty($postStr)){
//            $this->logger("R \r\n".$postStr);
            trace("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK1");
            trace($postStr);
            trace("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK2");


            $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;
                case "image":
                    $result = $this->receiveImage($postObj);
                    break;
                case "location":
                    $result = $this->receiveLocation($postObj);
                    break;
                case "voice":
                    $result = $this->receiveVoice($postObj);
                    break;
                case "video":
                case "shortvideo":
                    $result = $this->receiveVideo($postObj);
                    break;
                case "link":
                    $result = $this->receiveLink($postObj);
                    break;
                default:
                    $result = "unknown msg type: ".$RX_TYPE;
                    break;
            }
//            $this->logger("T \r\n".$result);
            trace("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK3");
            trace($result);
            trace("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK4");
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    //接收事件消息
    private function receiveEvent($object)
    {
        $content = "";
        switch ($object->Event)
        {
            case "subscribe":
                $content = "欢迎关注土好网——直接连接买卖双方开发商董事长的二手地直卖网。";
                break;
            case "unsubscribe":
                $content = "取消关注";
                break;
            case "CLICK":
                switch ($object->EventKey)
                {
                    case "kefu98":
//                        $content = '我要返回图片';
//                        break;
//                        $content = array();
//                        $content[] = array("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
//                        break;
                        $content = array();
                        $content[] = array("MediaId"=>"huNJ_LxG8vmFunz2Hjeb73X1IS02pu0jslBK24HAhSqi3bw2ZTCCYwKU2PaIer5n");
                        break;
                    default:
                        $content = '';
                        break;
                }
                break;
            case "pic_weixin":
                $content = "相册发图:数量 ".$object->SendPicsInfo->Count;
                break;
            default:
                $content = "receive a new event: ".$object->Event;
                break;
        }

        if(is_array($content)){
//            $result = $this->transmitNews($object, $content);
            $result = $this->transmitImage($object, $content);
        }else{
            $result = $this->transmitText($object, $content);
        }
        return $result;
    }

    //接收文本消息
    private function receiveText($object)
    {
        $keyword = trim($object->Content);

        //自动回复模式
        if (strstr($keyword, "文本")){
            $content = "这是个文本消息";
        }else if (strstr($keyword, "表情")){
            $content = "微笑:/::)\n乒乓:/:oo\n中国:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335);
        }else if (strstr($keyword, "单图文")){
            $content = array();
            $content[] = array("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
        }else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){
            $content = array();
            $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
            $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
            $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
        }else if (strstr($keyword, "音乐")){
            $content = array();
            $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://mascot-music.stor.sinaapp.com/zxmzf.mp3", "HQMusicUrl"=>"http://mascot-music.stor.sinaapp.com/zxmzf.mp3");
        }else{
            $content = date("Y-m-d H:i:s",time())."\nOpenID:".$object->FromUserName."\n技术支持 方倍工作室";
            // $content = "";
        }

        if(is_array($content)){
            if (isset($content[0])){
                $result = $this->transmitNews($object, $content);
            }else if (isset($content['MusicUrl'])){
                $result = $this->transmitMusic($object, $content);
            }
        }else{
            $result = $this->transmitText($object, $content);
        }
        return $result;
    }

    //接收图片消息
    private function receiveImage($object)
    {
        $content = array("MediaId"=>$object->MediaId);
        $result = $this->transmitImage($object, $content);
        return $result;
    }

    //接收位置消息
    private function receiveLocation($object)
    {
        $content = "你发送的是位置,经度为:".$object->Location_Y.";纬度为:".$object->Location_X.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    //接收语音消息
    private function receiveVoice($object)
    {
        if (isset($object->Recognition) && !empty($object->Recognition)){
            $content = "你刚才说的是:".$object->Recognition;
            $result = $this->transmitText($object, $content);
        }else{
            $content = array("MediaId"=>$object->MediaId);
            $result = $this->transmitVoice($object, $content);
        }
        return $result;
    }

    //接收视频消息
    private function receiveVideo($object)
    {
        $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
        $result = $this->transmitVideo($object, $content);
        return $result;
    }

    //接收链接消息
    private function receiveLink($object)
    {
        $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    //回复文本消息
    private function transmitText($object, $content)
    {
        if (!isset($content) || empty($content)){
            return "";
        }

        $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

        return $result;
    }


    //回复图文消息
    private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return "";
        }
        $itemTpl = "        <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
        </item>
";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <ArticleCount>%s</ArticleCount>
    <Articles>
$item_str    </Articles>
</xml>";

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

    //回复音乐消息
    private function transmitMusic($object, $musicArray)
    {
        if(!is_array($musicArray)){
            return "";
        }
        $itemTpl = "<Music>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <MusicUrl><![CDATA[%s]]></MusicUrl>
        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
    </Music>";

        $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);

        $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[music]]></MsgType>
    $item_str
</xml>";

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

    //回复图片消息
    private function transmitImage($object, $imageArray)
    {
        $itemTpl = "<Image>
        <MediaId><![CDATA[%s]]></MediaId>
    </Image>";

//        $item_str = sprintf($itemTpl, 'huNJ_LxG8vmFunz2Hjeb73X1IS02pu0jslBK24HAhSqi3bw2ZTCCYwKU2PaIer5n');
        $item_str = sprintf($itemTpl, $imageArray['MediaId']);

        $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[image]]></MsgType>
    $item_str
</xml>";

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

    //回复语音消息
    private function transmitVoice($object, $voiceArray)
    {
        $itemTpl = "<Voice>
        <MediaId><![CDATA[%s]]></MediaId>
    </Voice>";

        $item_str = sprintf($itemTpl, $voiceArray['MediaId']);
        $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[voice]]></MsgType>
    $item_str
</xml>";

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

    //回复视频消息
    private function transmitVideo($object, $videoArray)
    {
        $itemTpl = "<Video>
        <MediaId><![CDATA[%s]]></MediaId>
        <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
    </Video>";

        $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);

        $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[video]]></MsgType>
    $item_str
</xml>";

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

    //日志记录
//    private function logger($log_content)
//    {
//        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 = 1000000;
//            $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('Y-m-d H:i:s')." ".$log_content."\r\n", FILE_APPEND);
//        }
//    }
}
?>
阅读全文

lnmp执行定时任务抓取文章信息并且入库(ThinkPHP5定时任务配合修罗BBS)(第一版初稿)

案例站点:传送门

定时任务脚本

vi /root/hello.sh

shell脚本要写环境变量才能生效

#!/bin/bash
export PATH="/usr/local/php/bin:$PATH"
export PATH="/:$PATH"
cd /mydata/catch/
php think test
echo "hello word!"

查看执行过的定时任务

tail -f /var/log/cron

查看定时任务(1分钟执行一次,执行定时任务的时候用source 环境变量才会生效)

[root@VM_0_15_centos /]# crontab -e
*/1 * * * * /usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &
*/1 * * * *  source  /root/hello.sh

查看PHP定时脚本

<?php
/**
 * Created by PhpStorm.
 * User: cwwx0
 * Date: 2019/6/7
 * Time: 21:52
 */

namespace app\index\controller;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use QL\QueryList;
use think\Db;

class Test extends Command
{
    protected function configure()
    {
        $this->setName('test')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {

        $url = 'https://it.ithome.com/ityejie/';
// 元数据采集规则
        $rules = [
            'title' => ['h2>a','text'],
            'link' => ['h2>a','href'],
            'img' => ['.list_thumbnail>img','src'],
            'desc' => ['.memo','text']
        ];
        // 切片选择器
        $range = '.ulcl>li:gt(0)';
        $rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData()->map(function($item){
            $qlde = QueryList::get($item['link']);
            $rtdetail = [];
            $rtdetail['title'] = $qlde->find('h1')->text();
            $rtdetail['author'] = $qlde->find('#author_baidu>strong')->text();
            $rtdetail['createtime'] = $qlde->find('#pubtime_baidu')->text();
            $rtdetail['content'] = $qlde->find('.post_content')->html();
            $item['content']=$rtdetail;
            return $item;
        })->all();

        foreach (array_reverse($rt) as $val){

            $check=Db::name('thread')->where('subject',$val['title'])->find();
            if($check){
                continue;
            }
            $int=array();
            $int['fid']=5;
            $int['uid']=1;
            $int['userip']=1881148765;
            $int['subject']=$val['title'];
            $int['create_date']=strtotime($val['content']['createtime']);
            $int['last_date']=strtotime($val['content']['createtime']);
            $tid=Db::name('thread')->insertGetId($int);


            $second=array();
            $second['uid']=1;
            $second['tid']=$tid;
            Db::name('mythread')->insert($second);

            $thirdpost=array();
            $thirdpost['tid']=$tid;
            $thirdpost['uid']=1;
            $thirdpost['isfirst']=1;
            $thirdpost['create_date']= strtotime($val['content']['createtime']);
            $thirdpost['userip']=1881148765;
            $thirdpost['images']=0;
            $thirdpost['files']=0;
            $thirdpost['doctype']=0;
            $thirdpost['quotepid']=0;
            $thirdpost['r_f_c']=0;
            $thirdpost['r_f_a']=0;
            $thirdpost['likes']=0;
            $thirdpost['repeat_follow']='';
            $thirdpost['message']=$val['content']['content'];
            $thirdpost['message_fmt']=$val['content']['content'];
            $pid=Db::name('post')->insertGetId($thirdpost);
            Db::name('thread')->where('tid',$tid)->update(array('firstpid'=>$pid,'lastpid'=>$pid));

            $output->writeln("*****************************************:");
            echo '文章'.$val['title'].'导入成功';
            $output->writeln("*****************************************:");
        }
//        print_r($rt);
        $output->writeln("TestCommand:");
    }
}

手动执行一次看一下结果如下所示

[root@VM_0_15_centos /]# /root/hello.sh
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/http.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/http.so: cannot open shared object file: No such file or directory in Unknown on line 0
*****************************************:
文章震撼体又来!微信助手辟谣“高考满分作文来了,震撼13亿人...”导入成功*****************************************:
*****************************************:
文章华为:2019年年内或无法完成超越三星的目标导入成功*****************************************:
*****************************************:
文章快狗打车否认裁员50%:调整比例未超员工总数的3.5%导入成功*****************************************:
*****************************************:
文章中国铁路12306出故障,大量用户反馈无法支付导入成功*****************************************:
*****************************************:
文章国资委:三家运营商要进一步加强合作,避免5G重复投资导入成功*****************************************:
*****************************************:
文章四部委联合开展专项整治活动:将处罚并曝光违法违规网站导入成功*****************************************:
*****************************************:
文章回应格力举报,奥克斯以损害商誉为由报案:宁波警方已受理导入成功*****************************************:
*****************************************:
文章安卓绿色联盟分析八大新闻类App内存占用情况导入成功*****************************************:
*****************************************:
文章工商银行:正式推出首家基于5G应用的新型智能网点导入成功*****************************************:
*****************************************:
文章菜鸟联手百世、申通、圆通率先上线新版电子面单导入成功*****************************************:
*****************************************:
文章阿里云与慧联无限发布首款LoRa室内安防套装产品 可30秒内完成安装导入成功*****************************************:
*****************************************:
文章618物流大提速,苏宁易购开启五大新仓库导入成功*****************************************:
*****************************************:
文章CES Asia 2019智慧5G小店被定义 苏宁联通携手共进5G时代导入成功*****************************************:
*****************************************:
文章618苏宁汽车O2O服务见效,自营轮胎销售上涨352%导入成功*****************************************:
TestCommand:
阅读全文

修罗BBS接口数据新增规则参考下面的数据新增规律即可

INSERT INTO `bbs_mythread` (`uid`, `tid`) VALUES
(1,    14),
INSERT INTO `bbs_post` (`tid`, `pid`, `uid`, `isfirst`, `create_date`, `userip`, `images`, `files`, `doctype`, `quotepid`, `message`, `message_fmt`, `repeat_follow`, `r_f_c`, `r_f_a`, `likes`) VALUES
(14,    14,    1,    1,    1560182492,    1881148765,    0,    0,    0,    0,    '<p>还行吧</p>',    '<p>还行吧</p>',    '',    0,    0,    0),

INSERT INTO `bbs_thread` (`fid`, `tid`, `top`, `uid`, `userip`, `subject`, `create_date`, `last_date`, `views`, `posts`, `images`, `files`, `mods`, `closed`, `firstpid`, `lastuid`, `lastpid`, `likes`) VALUES
(1,    14,    0,    1,    1881148765,    '看一看你是怎么',    1560182492,    1560182492,    1,    0,    0,    0,    0,    0,    14,    0,    14,    0),


INSERT INTO `bbs_mythread` (`uid`, `tid`) VALUES
(1,    15);

INSERT INTO `bbs_post` (`tid`, `pid`, `uid`, `isfirst`, `create_date`, `userip`, `images`, `files`, `doctype`, `quotepid`, `message`, `message_fmt`, `repeat_follow`, `r_f_c`, `r_f_a`, `likes`) VALUES
(15,    15,    1,    1,    1560182951,    1881148765,    0,    0,    0,    0,    '<p>122222222</p>',    '<p>122222222</p>',    '',    0,    0,    0);
INSERT INTO `bbs_thread` (`fid`, `tid`, `top`, `uid`, `userip`, `subject`, `create_date`, `last_date`, `views`, `posts`, `images`, `files`, `mods`, `closed`, `firstpid`, `lastuid`, `lastpid`, `likes`) VALUES
(1,    15,    0,    1,    1881148765,    '你好',    1560182951,    1560182951,    0,    0,    0,    0,    0,    0,    15,    0,    15,    0);


INSERT INTO `bbs_mythread` (`uid`, `tid`) VALUES
(1,    16);

INSERT INTO `bbs_post` (`tid`, `pid`, `uid`, `isfirst`, `create_date`, `userip`, `images`, `files`, `doctype`, `quotepid`, `message`, `message_fmt`, `repeat_follow`, `r_f_c`, `r_f_a`, `likes`) VALUES
(16,    16,    1,    1,    1560182951,    1881148765,    0,    0,    0,    0,    '<p>2222</p>',    '<p>3333</p>',    '',    0,    0,    0);

INSERT INTO `bbs_thread` (`fid`, `tid`, `top`, `uid`, `userip`, `subject`, `create_date`, `last_date`, `views`, `posts`, `images`, `files`, `mods`, `closed`, `firstpid`, `lastuid`, `lastpid`, `likes`) VALUES
(1,    16,    0,    1,    1881148765,    '你4好',    1560182951,    1560182951,    0,    0,    0,    0,    0,    0,    16,    0,    16,    0);
阅读全文

Lnmp 环境下 使用php -v与phpinfo()版本不一致问题

1.为什么会出现这个问题?

1.1  php -v是查看linux系统的默认php版本,phpinfo()是跑的web版本如果不修改默认版本和web版本一致的话,可能会出现以下问题:在windows下用php7.1.7版本用laravel开发一个项目,然后git到服务器上,composer install一直报php版本不够,但是phpinfo()在浏览器上面看到的是php7.1.7;解决方法:

2.查看自己php7.1.7的位置

find ./ -name php

2019-06-07T14:54:47.png

我是用的lnmp集成包安装的,所以我的web版本在/usr/local/php目录下

3.在命令行输入:export PATH="/usr/local/php/bin:$PATH"

2019-06-07T14:55:23.png

4.用php -v查看发现版本正确,然后composer install项目就不会报版本不够了

5注意这只是临时添加

永久添加:

添加全局变量在/etc/profile文件中

vi /etc/profile

添加:

2019-06-07T14:56:18.png

export PATH=/usr/local/php/bin:$PATH

注意:= 即等号两边不能有任何空格

阅读全文