0

我有这个网址,但是当我运行 curl 函数时,我无法得到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;
  }
4

1 回答 1

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;
}  
于 2012-08-26T00:45:55.473 回答