Exemplo n.º 1
0
function createDoc()
{
    global $titulos, $autores, $categorias, $tags;
    $titulo = getRandomArrayItem($titulos);
    $autor = getRandomArrayItem($autores);
    $categoria = getRandomArrayItem($categorias);
    $articleTags = array();
    $numOfTags = rand(1, 5);
    for ($j = 0; $j < $numOfTags; $j++) {
        $tag = getRandomArrayItem($tags);
        if (!in_array($tag, $articleTags)) {
            array_push($articleTags, $tag);
        }
    }
    $publishedAt = new MongoDate(getRandomTimestamp());
    $rating = mt_rand(1, 10);
    return array('titulo' => $titulo, 'autor' => $autor, 'categoria' => $categoria, 'tags' => $articleTags, 'publicado_el' => $publishedAt, 'rating' => $rating);
}
function createDoc()
{
    global $titles, $authors, $categories, $tags;
    $title = getRandomArrayItem($titles);
    $author = getRandomArrayItem($authors);
    $category = getRandomArrayItem($categories);
    $articleTags = array();
    $numOfTags = rand(1, 5);
    for ($j = 0; $j < $numOfTags; $j++) {
        $tag = getRandomArrayItem($tags);
        if (!in_array($tag, $articleTags)) {
            array_push($articleTags, $tag);
        }
    }
    $publishedAt = new MongoDate(getRandomTimestamp());
    $rating = mt_rand(1, 10);
    return array('title' => $title, 'author' => $author, 'category' => $category, 'tags' => $articleTags, 'published_at' => $publishedAt, 'rating' => $rating);
}
<?php

require 'dbconnection.php';
$mongo = DBConnection::instantiate();
$articles = $mongo->getCollection('articles');
$articleIds = array();
foreach ($articles->find(array(), array('_id' => TRUE)) as $article) {
    array_push($articleIds, (string) $article['_id']);
}
function getRandomArrayItem($array)
{
    $length = count($array);
    $randomIndex = mt_rand(0, $length - 1);
    return $array[$randomIndex];
}
echo 'Simulating blog post reading...';
while (1) {
    $id = getRandomArrayItem($articleIds);
    //change the value of $url accordingly on your machine
    $url = sprintf('http://localhost:8888/mongodb/chapter5/blog.php?id=%s', $id);
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $url);
    curl_setopt($curlHandle, CURLOPT_HEADER, false);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    curl_exec($curlHandle);
    curl_close($curlHandle);
}