doubi1797 2014-09-04 22:54
浏览 29
已采纳

使用PHP显示“Google Books”项目

I'm studying a way to use the Google Books API. Using this code have the expected result.

$page = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=julio+verne&maxResults=40");

$data = json_decode($page, true);
for($a = 0 ; $a <= 39 ; $a++) {
$img = $data['items'][$a]['volumeInfo']['imageLinks']['thumbnail'];
print '<img src="'.$img.'" alt="ScanLine"/>';
echo '<br>';
echo "Title = " . $data['items'][$a]['volumeInfo']['title']; 
echo '<br>';
echo "Authors = " . @implode(",", $data['items'][$a]['volumeInfo']['authors']);
echo '<br>';
echo "Editora = " . $data['items'][$a]['volumeInfo']['publisher'];
echo '<br>';
echo "id = " . $data['items'][$a]['id'];
echo '<br>';
echo "Resumo = ";
echo "<p>" . $data['items'][$a]['volumeInfo']['description'];
echo '</p><br>';

}

On the other hand, implementing and using the code form the code does not work. My page with form have this code:

<form action="action.php" method="POST">
        <div class="form-group">
                <div class="campos">
                        <label>
                         Search
                        </label>
                        <input type="text" name="search"  style="margin-right: 10px; width:250px; float:left" class="input-field" placeholder="Title, Author..."  />    
                        <input type=hidden name=numResults value="&maxResults=40">
                        <button type="submit" id="search" class="btn btn-default">Search</button>
                </div>
        </div>
</form>

And my action have this code:

 $var1 = "https://www.googleapis.com/books/v1/volumes?q=";
 $var2 = urlencode($_POST['search']);
 $var3 = "&maxResults=40";
 $str = str_replace(" ", "+", $var2);
 $page = $var1.$str.$var3;
 $data = json_decode($page, true);
 echo $page;
 echo '<br>';
 for($a = 0 ; $a <= 39 ; $a++) {
 $img = $data['items'][$a]['volumeInfo']['imageLinks']['thumbnail'];
 print '<img src="'.$img.'" alt="ScanLine"/>';
 echo '<br>';
 echo "Title = " . $data['items'][$a]['volumeInfo']['title']; 
 echo '<br>';
 echo "Authors = " . @implode(",", $data['items'][$a]['volumeInfo']['authors']);
 echo '<br>';
 echo "Editora = " . $data['items'][$a]['volumeInfo']['publisher'];
 echo '<br>';
 echo "Resumo = ";
 echo "<p>" . $data['items'][$a]['volumeInfo']['description'];
 echo '</p><br>';
  }

What must be wrong? Any advise? Thanks from Brazil

  • 写回答

1条回答 默认 最新

  • dongtan3395 2014-09-04 23:01
    关注

    You are not calling file_get_contents() in the second example. So $page is just the url.

    $page = file_get_contents($var1.$str.$var3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度