dongxin8392 2013-05-31 11:13
浏览 35
已采纳

使用PHP返回HTML图像搜索结果

I know how we can use the Google API to return image results in AJAX, but I want to be able to return images for a specific query and then output them in to HTML on my page.

For example:

http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=sausages

Returns results with infomation and images about the top 10 results for the keyword sausages.

How can I query this url to output the images and titles of the images on my page using PHP in HTML.

I am using the following at the top of the function to return the title:

$tit = get_the_title();

Then I am apending it here:

$json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='.$tit.'');

But it won't recognize the title

  • 写回答

4条回答 默认 最新

  • doukong1897 2013-05-31 11:21
    关注
    function get_url_contents($url) {
        $crl = curl_init();
    
        curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
        curl_setopt($crl, CURLOPT_URL, $url);
        curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
    
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
    }
    
    $json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=sausages');
    
    $data = json_decode($json);
    
    foreach ($data->responseData->results as $result) {
        $results[] = array('url' => $result->url, 'alt' => $result->title);
    }
    
    print_r($results);
    

    Output:

    Array
    (
        [0] => Array
            (
                [url] => http://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/Salchicha_oaxaque%25C3%25B1a.png/220px-Salchicha_oaxaque%25C3%25B1a.png
                [alt] => Sausage - Wikipedia, the free encyclopedia
            )
    
        [1] => Array
            (
                [url] => http://upload.wikimedia.org/wikipedia/commons/c/c1/Reunion_sausages_dsc07796.jpg
                [alt] => File:Reunion sausages dsc07796.jpg - Wikimedia Commons
            )
    
        [2] => Array
            (
                [url] => http://1.bp.blogspot.com/-zDyoLPoM1Zg/ULXDPba_2iI/AAAAAAAAAAs/QzfNNmDFmzc/s1600/shop_sausages.jpg
                [alt] => Maik's Yummy German Sausage
            )
    
        [3] => Array
            (
                [url] => http://sparseuropeansausage.com/images/sausage-web/sausagesBiggrilling2.jpg
                [alt] => Spar's European Sausage Shop
            )
    
    )
    

    Showing the images:

    <?php foreach($results as $image): ?>
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/><br/>
    <?php endforeach; ?>
    

    Edit after comments:

    $url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' . get_the_title(); 
    
    $json = get_url_contents($url);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂