Yahoo php sdk getContacts() intermittently works

断了今生、忘了曾经 提交于 2020-01-06 14:58:15

问题


I'm using the Yahoo! Social SDK to allow a user to authorize and then get a list of their contacts. I've setup the app to allow for the contact data to be read and this is verified when authenticating.

The authentication works because I can the profile using getProfile() on every single page load. The getContacts() is the problem though as 95% of the time it returns false which is not correct.

Am I doing something wrong with the request tokens that means getContacts() doesn't have the correct permission to run successfully or do Yahoo have some sort of strange caching issue with this query? It's even harder with the distinct lack of documentation from them regarding their api and php, is there another new library I can use to achieve this? I know it's possible because I can use a working version on the "AirBnb Invite a friend" webpage.

This is the code I'm using, it's written using CodeIgniter so that explains the syntax.

public function yahoo() {

    $oauthapp = new YahooOAuthApplication(DEV_OAUTH_CONSUMER_KEY, DEV_OAUTH_CONSUMER_SECRET, DEV_OAUTH_APP_ID, DEV_OAUTH_DOMAIN);

    if($this->session->userdata('yahoo_oauth_access_token')){

        $oauthapp->token = YahooOAuthAccessToken::from_string($this->session->userdata('yahoo_oauth_access_token'));

        $profile = $oauthapp->getProfile();
        $contacts = $oauthapp->getContacts(0, 1000);

        if($profile)
           print_r($profile);
        else
            echo "No profile / error";

        if($contacts)
           print_r($contacts);
        else
            echo "No contacts / error";

    }
    elseif(!$this->input->get()) {

        $request_token = $oauthapp->getRequestToken(DEV_OAUTH_DOMAIN);
        $this->session->set_userdata('request_token', json_encode($request_token));
        $redirect_url = $oauthapp->getAuthorizationUrl($request_token);

        redirect($redirect_url);
    }
    else {

        $request_token = json_decode($this->session->userdata('request_token'));
        $oauthapp->token = $oauthapp->getAccessToken($request_token, $this->input->get('oauth_verifier'));

        $this->session->set_userdata('yahoo_oauth_access_token', $oauthapp->token->to_string());

        redirect("/index/yahoo");
    }
}

回答1:


Finally found the issue after a long long search, almost certain there will be others with this same issue as it's got no error outputting.

The issue was Curl in the library exceeding the runtime of 10 seconds, if this happens it just timeouts and the script ends. There were errors in my error_log but nothing on screen.

If you increase the timeouts on line 65 and 66 in YahooCurl.class.php then this fixes the issue.

  /**
   * Fetches an HTTP resource
   *
   * @param string $url    The request url
   * @param string $params The request parameters
   * @param string $header The request http headers
   * @param string $method The request HTTP method (GET, POST, PUT, DELETE, HEAD)
   * @param string $post   The request body
   *
   * @return string Response body from the server
   */
  public static function fetch($url, $params, $headers = array(), $method = self::GET, $post = null, $options = array())
  {
    $options = array_merge(array(
      'timeout'         => '10',
      'connect_timeout' => '10',
      'compression'     => true,
      'debug'           => true,
      'log'             => sys_get_temp_dir().'/curl_debug.log',
    ), $options);


来源:https://stackoverflow.com/questions/30919089/yahoo-php-sdk-getcontacts-intermittently-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!