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']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算