duanbei7035 2016-10-12 06:51
浏览 32

使用jquery load和php从外部网站抓取信息

I am trying to scrape a random word from a random word generating website. Through reading other forum posts it seems that jquery load is the way to go in conjunction with some short PHP. I've created a PHP file called "grabber.php" which contains one line:

<?php echo file_get_contents($_GET['url']); ?>

Then I call this in my javascript like so:

$("#word").load('grabber.php?url=http://watchout4snakes.com/wo4snakes/Random/RandomWord #result')

The tag holding the random word on the external site is result and the tag which I want my site to hold the data is word.

For some reason it isn't working at all. It will delete whatever is held in the word tag header. What am I doing wrong? Thanks!

If it helps, my html code is:

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="main.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src=timer.js></script>
</head>

<body>
<h1>Hello</h1>
<h2 id="time">The Timer is Here: </h2>
<h2 id="word">Word Goes Here</h2> </body>


</html>
  • 写回答

1条回答 默认 最新

  • dongxian3418 2016-10-12 06:58
    关注

    You can process the request purely through PHP, no need for the $_GET['url'] either.

    Within your grabber.php file, you can use cURL, or some library like Guzzle to make an HTTP request to watchout4snakes. When the request is received, you can use json_decode to turn the JSON string into an object. You can then send the word to the client using $('#word').load().

    Guzzle: https://github.com/guzzle/guzzle

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line