duandao7704 2013-05-11 16:39
浏览 43
已采纳

PHP内部的调用函数直到条件遇到

I have a function that I am creating that basically connects to the Google Plus API and Pulls information. Unfortunately in order to get more than a few results at a time you need to use the nextPageToken provided in the API callback. Here is what I have so far:

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();

If you can kind of see what I am trying to do here hopefully someone can help me out. I basically want to keep re-calling the parseResults() function with the provided nextPageToken until there have been 20 records processed.

  • 写回答

2条回答 默认 最新

  • dongpa9277 2013-05-14 15:32
    关注

    I was able to solve it with help from Lawrence above:

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题