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
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.
Link to comment
Share on other sites

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)
Link to comment
Share on other sites

I've never had so many replies on a forum in such a short space of time!

Here is the url:

http://www.geocities.com/avatar11235

index.php is the hello world file above.

At my end if i try to view it i get nothing but a blank page.

I'm going to try a different host.
Link to comment
Share on other sites

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. 
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote]needs to be the PHYSICAL address[/quote]

The file() function can except a url just fine. I really can't see anything wrong witht this code, though Im no regx expert.

Have you got error reporting enabled? That may give us a clue.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.