1

我有一个我正在创建的函数,它基本上连接到 Google Plus API 和 Pulls 信息。不幸的是,为了一次获得多个结果,您需要使用nextPageTokenAPI 回调中提供的。这是我到目前为止所拥有的:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function parseResults($nextPageToken = "") {
    $count = 0;
    $content = get_data('https://www.googleapis.com/plus/v1/activities?query=%23throughglass&maxResults=10&orderBy=recent&fields=items(actor(displayName%2Curl)%2Cid%2Cobject(actor%2Cattachments)%2Curl%2Cverb)%2CnextPageToken&pageToken=' . $nextPageToken . '&key=YOUR API KEY HERE');
    $posts = json_decode($content);
    $token = $posts->nextPageToken;
    foreach ($posts->items as $value){
        $id = $value->id;
        $id_query = mysql_query("SELECT id FROM images WHERE id = '$id'");
        if ($value->verb != "post") continue;
        if (mysql_num_rows($id_query) > 0) continue;
        echo $value->actor->displayName . "<br />";
        $count++;
    }
    if ($count < 20){
        parseResults($token);
    }else{
        break;
    }

}
parseResults();

如果你能看到我在这里想要做什么,希望有人能帮助我。我基本上想继续使用提供的 nextPageToken 重新调用 parseResults() 函数,直到处理了 20 条记录。

4

2 回答 2

1

在上面的劳伦斯的帮助下,我能够解决它:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}


function parseResults($nextPageToken = "", $count) {
    $content = get_data('https://www.googleapis.com/plus/v1/activities?query=%23throughglass&maxResults=10&orderBy=recent&fields=items(actor(displayName%2Curl)%2Cid%2Cobject(actor%2Cattachments)%2Curl%2Cverb)%2CnextPageToken&pageToken=' . $nextPageToken . '&key={YOUR_API_KEY}');
    $posts = json_decode($content);
    $token = $posts->nextPageToken;
    foreach ($posts->items as $value){
        $id = $value->id;
        $name = $value->actor->displayName;
        $profile = $value->actor->url;
        $post = $value->url;
        $post_text = $value->object->attachments[0]->displayName;
        $image_small = $value->object->attachments[0]->image->url;
        $image_full = $value->object->attachments[0]->fullImage->url;

        $id_query = mysql_query("SELECT id FROM images WHERE id = '$id'");
        if (mysql_num_rows($id_query) > 0) continue;

        if ($value->verb != "post") continue;
        if ($value->object->attachments[0]->image->url == "" || $value->object->attachments[0]->fullImage->url == "") continue;
        if ($post_text != "#throughglass") continue;

        mysql_query("INSERT INTO images (id, 
                                        author_name, 
                                        author_url, 
                                        post_url, 
                                        post_text, 
                                        image_small, 
                                        image_full) VALUES (
                                        '$id', 
                                        '$name', 
                                        '$profile', 
                                        '$post', 
                                        '$post_text', 
                                        '$image_small', 
                                        '$image_full')");
        echo "<b>ID: </b>" . $id . "<br />";
        echo "<b>Name: </b>" . $value->actor->displayName . "<br />";
        echo "<b>Profile URL: </b>" . $value->actor->url . "<br />";
        echo "<b>Post URL: </b>" . $value->url . "<br />";
        echo "<b>Post Text: </b>" . $value->object->attachments[0]->displayName . "<br />";
        echo "<b>Image Small: </b>" . $value->object->attachments[0]->image->url . "<br />";
        echo "<b>Image Full: </b>" . $value->object->attachments[0]->fullImage->url . "<br /><br />";
        $count++;
    }
    if ($count < 100){
        parseResults($token, $count);
    }else{
        echo "<br /><br /><br />" . $token;
        break;
    }

}
parseResults("", 0);
于 2013-05-14T15:32:25.533 回答
0

我想你想计算递归调用的数量。

您可以通过将函数包装到一个类中并使用类属性来实现这一点,但最简单的方法是向函数添加一个参数parseResult并在下一次调用之前递增它。

于 2013-05-11T17:11:16.690 回答