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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?