0

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.