谷歌建议随机PHP

时间:2019-03-07 08:51:01

标签: php

我想从Google的建议中获得随机建议; 但是我看到了

'selena','selena gomez','selena','selena gomez age','selena gomez歌曲','selena gomez电影','selena gomez puma','selena gomez 2018','selena gomez新歌曲”,“瑟琳娜·戈麦斯net woth”,“瑟琳娜·戈麦斯专辑”

不能随机工作:

$suggURL =
    'http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q=' .
    urlencode("selena");
$context = stream_context_create(array(
    "http" => array(
        "header" =>
            "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
    ),
));
$data = file_get_contents($suggURL, false, $context);
//$data=$this->get_web_page( $suggURL );
$data = preg_replace('/]/', '', $data); // remove numbers
$data = str_replace('[', "", $data);
$data = str_replace('r', "", $data);
$data = str_replace('"', "'", $data);

$data;
$a = ['' . $data . ''];
echo "<br/>";
echo $website = $a[mt_rand(0, count($a) - 1)];
"\n";

2 个答案:

答案 0 :(得分:1)

$data变量包含一个JSON字符串,因此您以错误的方式处理它。请改用json_decode()

<?php

$suggURL =
    'http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q=' .
    urlencode("selena");
$context = stream_context_create(array(
    "http" => array(
        "header" =>
            "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
    ),
));
$data = file_get_contents($suggURL, false, $context);

$a = json_decode($data);

echo "<br/>";
echo $a[1][mt_rand(0, count($a[1]) - 1)];
"\n";

?>

说明:

$data具有以下格式:

["selena",["selena gomez","selena","selena gomez age","selena gomez songs","selena movie","selena gomez movies","selena gomez net worth","selena death","selena songs","selena gomez back to you"]]

这是JSON格式,因此使用json_decode()可获得print_r($a);的结果:

Array ( [0] => selena [1] => Array ( [0] => selena gomez [1] => selena [2] => selena gomez age [3] => selena gomez songs [4] => selena movie [5] => selena gomez movies [6] => selena gomez net worth [7] => selena death [8] => selena songs [9] => selena gomez back to you ) )

您现在可以访问建议数组$a[1],并从echo $a[1][mt_rand(0, count($a[1]) - 1)];中获得随机建议

答案 1 :(得分:0)

请仔细检查数组是否正确处理,您可以使用以下代码从数组中选择随机值

<main>
  <div id="main-left"></div>
  <div id="main-right">
    <p>Play with the angles. This is unplanned it really just happens. A fan brush is a fantastic piece of equipment. Use it. Make friends with it. We have no limits to our world. We're only limited by our imagination. The very fact that you're aware of
      suffering is enough reason to be overjoyed that you're alive and can experience it. We don't have anything but happy trees here. This painting comes right out of your heart. Any little thing can be your friend if you let it be. Learn when to stop.
      You can create beautiful things - but you have to see them in your mind first. There's not a thing in the world wrong with washing your brush. These little son of a guns hide in your brush and you just have to push them out.</p>
  </div>
</main>

这是工作示例;

$rant = $data[1][ mt_rand(0, count($data[1])-1 ) ];