我正试图从一个随机单词生成网站刮取一个随机单词。通过阅读其他论坛帖子,似乎jquery load
是与一些简短的PHP结合使用的方式。我创建了一个名为"grabber.php"的PHP文件,其中包含一行:
<?php echo file_get_contents($_GET['url']); ?>
然后我在javascript中像这样调用它:
$("#word").load('grabber.php?url=http://watchout4snakes.com/wo4snakes/Random/RandomWord #result')
在外部网站上持有随机单词的标签是result
,我希望我的网站持有数据的标签是word
。
由于某种原因,它根本不起作用。它将删除word
标记头中保存的任何内容。我做错了什么?谢谢!
我的html代码是:
<!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>
您可以完全通过PHP处理请求,也不需要$_GET['url']
。
在grabber.php文件中,可以使用cURL或Guzzle之类的库向watchout4snakes发出HTTP请求。当接收到请求时,可以使用json_decode
将JSON字符串转换为对象。然后,您可以使用$('#word').load()
将单词发送到客户端。
狂饮:https://github.com/guzzle/guzzle