Help with PHP and shell_exec!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
o0110o
Forum Newbie
Posts: 5
Joined: Sun Feb 14, 2010 2:55 pm

Help with PHP and shell_exec!!!

Post by o0110o »

Hi, I've been working on a PHP script which is "supposed" to find an individuals weather based on their geolocation. This script uses "shell_exec".

I have checked my syntax and it is correct, but there is still something missing; for when I call on the script using:

Code: Select all

<form action='/weather.php' method='get'><br>
<input type='submit' name='submit' value='Check'><br>
</form>
I get a blank page :(

Here is my script:

Code: Select all

<?php
 
function getWeather() {
$ip = $_SERVER['REMOTE_ADDR']; 
$geo = shell_exec("/usr/bin/curl -s api.hostip.info/get_html.php?ip=$ip | /usr/bin/sed -e '1d;3d' -e's/C.*: \(.*\)/\1/' -e's|-|%2d|' -e's/ /%20/' -e\s/'/%27/'");
$getAddress = "http://www.google.com/ig/api?weather=$geo";
$xml_str = file_get_contents($getAddress,0);
$xml = new SimplexmlElement($xml_str);
$count = 0;
echo '<div id="weather">';
 
foreach($xml->weather as $item) {
 
        foreach($item->current_conditions as $new) {
 
            echo '<div class="weatherIcon">';
            echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/><br/>';
        echo $new->condition['data'];
            echo $new->temp_f['data'];
            echo $new->temp_c['data'];
            echo $new->humidity['data'];
            echo $new->wind_condition['data'];
            echo '</div>';
            }
 
        foreach($item->forecast_conditions as $new) {
 
            echo '<div class="weatherIcon">';
            echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/><br/>';
            echo $new->day_of_week['data'];
            echo $new->condition['data'];
            echo $new->low['data'];
            echo $new->high['data'];
            echo '</div>';
            }
 
    }
 
echo '</div>';
}
 
getWeather();
 
?>
I'm a bit of a noob when it comes to this, so any help would be greatly appreciated. Thanks.
Last edited by o0110o on Sun Feb 14, 2010 5:25 pm, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help with PHP and shell_exec!!!

Post by infolock »

I don't have sed installed so I can't really test this out..

However, I assume that your shell script has been tested on a terminal. I would try to execute that line of code

Code: Select all

 
/usr/bin/curl -s api.hostip.info/get_html.php?ip=$ip | /usr/bin/sed -e '1d;3d' -e's|C.*:||' -e's/...\(.*\)/\1/' -e's|-|%2d|' -e's/ /%20/' -e\s/'/%27/'
 
in a terminal and verify that results are being returned. If they are, then please post the returned results so we can diagnose further.

Of course, for $ip, you'll want to put in your local computer's address or something =P
o0110o
Forum Newbie
Posts: 5
Joined: Sun Feb 14, 2010 2:55 pm

Re: Help with PHP and shell_exec!!!

Post by o0110o »

infolock wrote:I don't have sed installed so I can't really test this out..

However, I assume that your shell script has been tested on a terminal. I would try to execute that line of code

Code: Select all

 
/usr/bin/curl -s api.hostip.info/get_html.php?ip=$ip | /usr/bin/sed -e '1d;3d' -e's|C.*:||' -e's/...\(.*\)/\1/' -e's|-|%2d|' -e's/ /%20/' -e\s/'/%27/'
 
in a terminal and verify that results are being returned. If they are, then please post the returned results so we can diagnose further.

Of course, for $ip, you'll want to put in your local computer's address or something =P

Okay so I input my command through SSH and it I got positive results:

Code: Select all

ST.%20JOHN%27S,%20NL
It did what it was supposed to do, which is take the location and encode the results for URL input.

I'm still not sure as to why I cannot get any output returned when this script is executed in a browser :(
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help with PHP and shell_exec!!!

Post by infolock »

did you execute this command as www ? also, for your shell exec, try piping out the results into a results.txt file. make sure that it's getting the data you are looking for.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help with PHP and shell_exec!!!

Post by infolock »

did you execute this command as www ? also, for your shell exec, try piping out the results into a results.txt file. make sure that it's getting the data you are looking for.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help with PHP and shell_exec!!!

Post by infolock »

did you execute this command as www ? also, for your shell exec, try piping out the results into a results.txt file. make sure that it's getting the data you are looking for.
o0110o
Forum Newbie
Posts: 5
Joined: Sun Feb 14, 2010 2:55 pm

Re: Help with PHP and shell_exec!!!

Post by o0110o »

infolock wrote:did you execute this command as www ? also, for your shell exec, try piping out the results into a results.txt file. make sure that it's getting the data you are looking for.
@infolock The ^above^ command works with or without "www".
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help with PHP and shell_exec!!!

Post by infolock »

Jeez, can someone delete those multiple posts?

anyways, I meant did you execute as the www user? what is the permissions to the shell tools you're trying to execute.

What i'm getting at, is it feels as though the www user doesn't have the permissions to execute these files. Otherwise, there would be input. I would also try echoing out the $geo variable to the screen. Also, again, I'd try piping the results to a flat file and make sure that they are being generated when you execute this via the browser. I realize when you do it via command line you get data. But maybe add another command in your $geo string to pipe to the file ;)

Edit: And if you are getting data from the $geo variable, start echoing out the data you're assigning to the other variables/arrays until you find the one that is being returned as blank.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help with PHP and shell_exec!!!

Post by infolock »

One more thing. I just ran your code but instead of this for the geo variable:

Code: Select all

 
$geo = shell_exec("/usr/bin/curl -s api.hostip.info/get_html.php?ip=$ip | /usr/bin/sed -e '1d;3d' -e's/C.*: \(.*\)/\1/' -e's|-|%2d|' -e's/ /%20/' -e\s/'/%27/'");
 
I changed it to your result you posted earlier:

Code: Select all

 
$geo = "ST.%20JOHN%27S,%20NL";
 
When I execute your script now, I get the results instead of a blank page. I think it has to do with one of these issues:
1) you don't have curl or sed installed
2) the www user/group doesn't have access to curl/sed
3) the path is wrong to curl/sed

I hope this helps.
Post Reply