2012-08-25 37 views
0

我有這個網址,但是當我運行卷曲功能,我不能得到access token:任何想法?空access_token

$url = "https://graph.facebook.com/oauth/access_token?client_id=xx16796&redirect_uri=http%3A%2F%2Flocalhost%2Fcake%2Fusers%2Flogin&client_secret=f70f01e76xxxxxebbbb9dfd7e&code=AQAD2Cfkx_RryGAQ5w5fHPelQFCOqIJRnw1gg8DGFpw..." 

/** 
     * Call to Facebook to get access token 
     */ 
     if (($response = $this->_connect($url)) !== false) { 
     parse_str($response, $response); 
      // i should stay here. 
     } 

    /** 
    * Something went wrong! todo 
    */ 
    if (empty($response['access_token'])) { 
    // i am here. 
    return false; 
    } 

public function _connect($url) 
    { 
    $response = false; 

    if (is_callable('curl_init')) { 

     $curl = curl_init($url); 

     if (is_resource($curl) === true) { 
     curl_setopt($curl, CURLOPT_FAILONERROR, true); 
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

     /** 
     * Attempt to execute the session until we get a result OR 
     * the number of maximum attempts has been reached 
     */ 
     $attempts = 3; 
     while (($response === false) && (--$attempts > 0)) { 
      $response = curl_exec($curl); 

      var_dump($curl); //resource(58, curl) 
      var_dump($response); //boolean false 

     } 

     curl_close($curl); 
     } else { 
     return false; 
     } 

/** 
* If cURL is not enabled, we use file_get_contents 
*/ 
    } else { 
     $response = @file_get_contents($url); 
    } 

    return $response; 
    } 

回答

1

我不知道到底是什麼問題。

但是這對我來說很好。

public function _connect($url) 
{ 
    $result = false; 
    $url = str_replace(' ', '%20', $url); 
    $curl = curl_init($url); 

    if (is_resource($curl) === true) 
    { 
     curl_setopt($curl, CURLOPT_FAILONERROR, true); 
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 

     $result = curl_exec($curl); 
     curl_close($curl); 
    } 

    return $result; 
}