dousongqiang2585 2013-08-05 19:51
浏览 330
已采纳

验证不存在的Twitter用户

I wrote a CodeIgniter app that inputs a Twitter screen name, and then fetches data about this user from the Twitter API 1.1. If the Twitter screen name does not exist, it returns the following error:

stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [message] => Sorry, that page does not exist [code] => 34 ) ) ) 

My question is, how do I validate/check for the above-mentioned error in my function get_user_data(...)? I want to return 34 if the page/user does not exist so that I can display an appropriate not found error view. Any assistance will be appreciated. Thanks in advance!

// ...
        $this->form_validation->set_rules('username', 'username', 'required');
        $username = $this->input->post('username'); 

        if($this->form_validation->run() === FALSE || $this->val_username($username) === FALSE)
        {
            $data = 'Validation Problems';          
            $this->view('search_page2',$data);
        }       
        else
        {               
            $twitter = new TwitterAPIExchange($settings);           
            $friends_list_url = 'https://api.twitter.com/1.1/users/show.json';                  
            $friends_list = json_decode($this->get_user_data($username, $twitter, $friends_list_url));
        }
        }
        }

        private function get_user_data($username, $twitter, $url)
    {
        $getfield = '?screen_name=' . $username;        
        $request_method = 'GET';          

        return $twitter->setGetfield($getfield)
                ->buildOauth($url, $request_method)
                ->performRequest();         
    }   

// ...
  • 写回答

1条回答 默认 最新

  • doumaogui5937 2013-08-05 20:05
    关注

    Change the following line to include true this will return an associative array which I find easier to work with

    $friends_list = json_decode($this->get_user_data($username, $twitter, $friends_list_url), true);
    

    Then all you need to do is reference the code like so

    $friends_list['errors'][0]['code']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?