Jump to content

how to run a php script?


zwkle

Recommended Posts

Hi,

I have a very simple requirement. I want to be able to use the php script below which was posted on another forum and which should tell me the number of google results for each member of a set of words in a text file.

I'm completely new to php. What's the quickest way of running the script? After a couple of hours of research I tried pasting the code into notepad, saving it as a .php file, and uploading it to a free geocities hosting account. I couldn't view it in IE though--I just got a blank page. Am I on the right track? I just want to be able to use it in the simplest way possible.

Andy


<?php

$file = file('path/to/your/list/of/words/to/search/for.txt');

$results = '';
foreach ($file as $line) {
    $data    = file_get_contents("http://www.google.com/search?hl=en&q=" . urlencode($line) . "&btnG=Google+Search");
    preg_match('/of about <b>([0-9,]*)?<\/b>/si', $data, $number);
    $results .= $line . " " . $number[1] . "\n";
}
echo $results;

?>
Link to comment
https://forums.phpfreaks.com/topic/29737-how-to-run-a-php-script/
Share on other sites

Well, first of all, it's blank right now because there's nothing it's searching for.

you need to change
path/to/your/list/of/words/to/search/for.txt
from the line:
$file = file('path/to/your/list/of/words/to/search/for.txt');
to the physical path to your .txt file that has your words you need to search for.
hi alchemy -- thanks. That was an incredibly quick reply.

I should have said that the file I uploaded to geocities was this one:

<html>
<head>
  <title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>

which is from the php manual. I saved this as a .php file before uploading it, but I'm not able to view it.

Is this something to do with php not being enabled on geocities? (I uploaded this simpler php file just as a test)
Ok, well I got a different host, phpnet.us, which does support php (as the name suggests) and I was able to upload the php test file and get it working.

I then replaced it with this file:

<html><head>
<title>PHP Test</title>
</head>
<body>
<?php 

$file = file('http://www.geocities.com/avatar11235/cars.txt');

$results = '';
foreach ($file as $line) {
     $data     = file_get_contents("http://www.google.com/search?hl=en&q=" . urlencode($line) . "&btnG=Google+Search");
    preg_match('/of about <b>([0-9,]*)?<\/b>/si', $data, $number);
     $results .= $line . " " . $number[1] . "\n";
}
echo $results;
  ?>
</body></html>

(cars.txt has three words in it on separate lines -- I uploaded it onto geocities because I couldn't work out the correct path if I uploaded it onto phpnet.us)

Now I get a blank page when I try to view the page. Does the code look right to you? It's meant to tell me how many google results there are for each entry in cars.txt. 
The <?php and >? should go at the beginning and end (respectfully) of the script always. Before the HTML etc, so that the server knows that it needs to process PHP. You will probably have to echo out the HTML though.
Whether that is the route cause of your problem is something else. Give it a try.
Ok,

now I'm really confused. I changed the line

$file = file('http://www.geocities.com/avatar11235/cars.txt'); to

$file = file('cars.txt');

and put the cars.txt file in the same directory as the php file which I have named index.php.
Now instead of a blank page I just get the three words in the text fill appearing all on one line.

If anyone's really keen to help me with this I can let you know the control panel username and password.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.