Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying everything 1

Status
Not open for further replies.

Netwrkengeer

IS-IT--Management
Apr 4, 2001
184
US
Ok I'm very new at this stuff, but I keep running through different commands and nothing is working. here is some code I wrote, the first piece is the html which is working fine. The second 2 pieces are 2 separate attempts to achieve my desired result.

My desired result is to send info to a website, and grab the raw html of the site.

/*HTML,eagle5.htm works fine*/
<html><head><title>SM2</title></head><body>
<!--the form attempts to send info via &quot;POST&quot; locally to sm2proc2.phtml-->
<FONT FACE=&quot;DAXLIGHT&quot; >
<TABLE COLSPEC=&quot;L20 L20&quot; >
<TR>
<TD>
<H1>
</H1>
</TD>
<TD>
Input your address.&nbsp;
<FORM ACTION=&quot;sm2proc2.phtml&quot; METHOD=post>
<PRE>
<!--the form variables street,city,state,zip, are unalterable-->
Street Address: <INPUT SIZE=30 NAME=&quot;street&quot;>
City: <INPUT SIZE=30 NAME=&quot;city&quot;>
State: <INPUT SIZE=3 NAME=&quot;state&quot;> Zip: <INPUT SIZE=6 NAME=&quot;zip&quot; <br>

<input type=submit name=&quot;geocode&quot; VALUE=&quot;request&quot;>
</PRE>
</FORM>
</TD>
</TR>
</TABLE>

</body></html>


/*PHP1, problem, won't echo/print contents of html from url, I test the url variables with echo and it printed the url with the inputed variables correctly, I then copied the correctly echo url into the address bar of my browser, and it gave me the page I wanted to see, so it works the way I want it to. now for the problem, with this code I get a log in page, not what I want.*/
<?php
/*
overview of code.
To submit info to a website via its URL, utilize variables from the POST form eagle5 which are concated into the url.
*/

readfile(&quot;
exit();

?>


/*PHP2, problem, won't echo/print contents of html from url, I test the url variables with echo and it printed the url with the inputed variables correctly, I then copied the correctly echo url into the address bar of my browser, and it gave me the page I wanted to see, so it works the way I want it to. now for the problem, with this code I get an echo of (0), not what I want.*/

<?php
/*
overview of code.
To submit info to a website via its URL, utilize variables from the POST form eagle5.htm which are concated into the url.
*/

/*opening connection to site, utilizing variables to access activated part of site i.e. $street*/
$fp = fsockopen (&quot; 80, $errno, $errstr, 30);
if (!$fp) {
echo &quot;$errstr ($errno)<br>\n&quot;;
} else {
fputs ($fp, &quot;GET / HTTP/1.0\r\n\r\n&quot;);
while (!feof($fp)) {
echo fgets ($fp,128);
}
fpassthru ($fp);
}

?>

/*PHPtest, this is the code I used to test the form and url.*/

<?php
echo $street.&quot;<br>&quot;;
echo $city.&quot;<br>&quot;;
echo $state.&quot;<br>&quot;;
echo $zip.&quot;<br>&quot;;
echo &quot; 80, $errno, $errstr, 30;
?>

I'm stumped,
thanks for any help you can give.
robert
 
Hey Robert, still at it, I see ;-)

Did you try urlencoding each variable before placing it in the query string? (See
And please try putting your code samples between the appropriate code brackets, so that Tek-tips doesn't mangle it. ( &#91;code&#93; ..your code here.. &#91;/code&#93; )
 
Forgot to mention...

The fact that it works from the browser might be because Internet Explorer does some automatic urlencoding when you place plain text on the query string, but in PHP you have to explicitly urlencode your query string variables.

Other than that, it looks to me like either PHP script should work.
 
I looked up urlencode on php.net and read about it in my book, and still have no idea how I would include it into my code, can you give me a hint here
 
I used the following (your first example) and corrected a syntax error you had (the ; after &street=&quot;)

<?php

readfile(&quot;
exit();

?>


I beilieve they require their test server to be run from a specific IP address. You should find that you won't be able to get the results you want because you aren't validated as a user on that box.

You should try to download the C client they have where you should be able to handle this app much easier and faster (just use popen() or shell_exec() to get results instead of having to make a call to their servers from your script).

Chad. ICQ: 54380631
online.dll
 
Ok, I want to post to the site and make the Php pretend to be a browser, here's my train of thought, and I could be wrong, but if I copy the url and paste it in the browser address bar manually I can get the info I want, so I want php to pretend to be copying and pasteing as if it where me with a mouse, then the info should come up.
or maybe not.
do you think what I'm thinking could be done with Php?
 
You just urlencode any specific variable by doing

Code:
urlencode($variable);

So your code should look like:

Code:
readfile(&quot;[URL unfurl="true"]http://www.geocode.com/scripts/eagle/eagle.pl?dummy=dummy&street=&quot;.urlencode($street).&quot;&city=&quot;.urlencode($city).&quot;&state=&quot;.urlencode($state).&quot;&zip=&quot;.urlencode($zip).&quot;&geocode=request&request=Address+Info&cmd=td_g&dum2=dummy/&quot;);[/URL]
 
rycamor is correct with regard to the urlencode, but the odd thing is that when you run the sample that is on their site, you get the map as well. Running it from a remote location, does not provide the map.

In any case, good catch rycamor and for Netwrkengeer, this is what your url would look like when you run rycamor's version:


and it works like a charm!

Sometimes I can just smack myself for missing the obvious.

Chad. ICQ: 54380631
online.dll
 
It works, thanks for all the help, you guys/girl are still the best,
thanks
Robert
 
Glad it worked, Rob. Just an additional note that might help: urlencode is only required for string variables. integers and floats should work just fine without encodeing them, since they don't include any special characters that interfere with URLs.

The problems of string manipulation and entry into databases are hard enough, but they take on an extra dimension when dealing with the web and HTTP protocol. PHP was written from the ground up with these problems in mind. Some other very useful PHP functions that you should research in this department are:

rawurlencode()
htmlentities()
htmlspecialchars()
htmlquotemeta()
addslashes() -- stripslashes()
addcslashes() -- stripcslashes()

I should note that probably the area of biggest concern after learning how URLs work, is to learn how to safely handle user input into forms, and how to safely display that input back in a form without breaking HTML compatibility. Some of the above functions are indispensable for this.
 
Oh I have a quick question, I researched the curl function, but I don't have it installed so I won't use it. anyway, I was reading a post and it talked about net_curl, and in the developers cook book, anyway, I can't find it in on php.net, have you used and what is it,

just a little info I grabbed,
Net_Curl PEAR class
Net_Curl ( [$url = &quot;&quot;] )
 
I use curl all the time in development. It is a wonderful tool. If you would like the binary, just let me know. It is not tied to any abnormal dependencies and you can place it anywhere you have access to on your server and then execute it via popen, exec, shell_exec, etc.

Chad. ICQ: 54380631
online.dll
 
yes I would like the binary for curl, you can email me or just post a link here,
my email is netwrkengeer@aol.com
thanks
 
ok I have more questions, I altered my code a little,and it still works but now I've added more to it and again I can't get the desired result. no I have 2 questions.
1. can php read the source code of an external website?
2. is their something wrong with my code.

I want my code to get the external site and then search through the html till it gets to line 103 then echo the contents of that line to the screen.


Code:
/*HTML, eagle11.htm*/

<html><head><title>SM2</title></head><body>
<!--the form attempts to send info via &quot;POST&quot; locally to sm2proc11.phtml-->
<FONT FACE=&quot;DAXLIGHT&quot; >
<TABLE COLSPEC=&quot;L20 L20&quot; >
  <TR>
    <TD>
      <H1>
      </H1>
    </TD>
    <TD>
      Input your address.&nbsp; 
      	<FORM ACTION=&quot;sm2proc11.phtml&quot; METHOD=post>
        <PRE>
<!--the form variables street,city,state,zip, are unalterable-->
	Street Address: <INPUT SIZE=30 NAME=&quot;street&quot;>
                  City: <INPUT SIZE=30 NAME=&quot;city&quot;>
                 State: <INPUT SIZE=3 NAME=&quot;state&quot;> Zip: <INPUT SIZE=6 NAME=&quot;zip&quot; <br>

             <input type=submit name=&quot;geocode&quot; VALUE=&quot;request&quot;>
        </PRE>
        </FORM>
    </TD>
  </TR>
</TABLE>

</body></html>

/************************************/

/*PHP, sm2proc11.phtml*/<?php 

/*overview of code.
To submit info to a website via its URL, utilize variables from the POST form eagle5.htm which are concated into the url.*/
 
/*read variables and get site info*/
$link = (&quot;[URL unfurl="true"]http://www.geocode.com/scripts/eagle/eagle.pl?dummy=dummy&street=&quot;.urlencode($street).&quot;&city=&quot;.urlencode($city).&quot;&state=&quot;.urlencode($state).&quot;&zip=&quot;.urlencode($zip).&quot;&geocode=request&request=Address+Info&cmd=td_g&dum2=dummy/&quot;);[/URL]

/* get contents of a file into a string*/
$filename = (&quot;$link&quot;);
$fd = readfile ($filename);
$contents = fread ($fd, 1024);
fclose ($fd);
 
/*looping through html of site to find line 103 to grab*/
 $line_s=0;
	while ($line_s=fgets($fd,1024)) {
		if ($line_s==103) {
		$right_line=$line;
		break;
	}
	$line_s++;
     }		
echo $line
?>

thanks again,
Robert
 
I'm also attempting to use this code, which is almost working, it seems that this code will not get me the line I want.

Code:
/*PHP, sm2proc12.phtml*/
<?php 
/* 
overview of code.
To submit info to a website via its URL, utilize variables from the POST form eagle5.htm which are concated into the url.
*/ 
/*read variables and get site info*/

$fd = file(&quot;[URL unfurl="true"]http://www.geocode.com/scripts/eagle/eagle.pl?dummy=dummy&street=&quot;.urlencode($street).&quot;&city=&quot;.urlencode($city).&quot;&state=&quot;.urlencode($state).&quot;&zip=&quot;.urlencode($zip).&quot;&geocode=request&request=Address+Info&cmd=td_g&dum2=dummy/&quot;);[/URL] 
$fd = implode ($fd, &quot;&quot;);

/*search and extract line*/
$line_num = 103;

$f_contents = file($fd);
$your_line = $f_contents[$line_num];

echo $your_line;

?>
 
Try this, it works fine. By the way, line 103 pulls in the following:
<DIV ALIGN=right><TT>XX:XX:XX.XXX[N|S]</TT></DIV>

for the Latitude in DD:MM:SSD format.

You will have to do a strip_tags($content); to get the text and not the HTML.

Code:
[sup]<?php

$filename = &quot;[URL unfurl="true"]http://www.geocode.com/scripts/eagle/eagle.pl?dummy=dummy&street=&quot;.urlencode($street).&quot;&city=&quot;.urlencode($city).&quot;&state=&quot;.urlencode($state).&quot;&zip=&quot;.urlencode($zip).&quot;&geocode=request&request=Address+Info&cmd=td_g&dum2=dummy/&quot;;[/URL]

$fc = @file($filename);

$full_contents = @implode(&quot;&quot;,$fc);
/*looping through html of site to find line 103 to grab*/
while (list($line, $content)=each($fc)) {
     if($line == 103) {
          echo $content;
     }
}
?>[/sup]

Chad.
ICQ: 54380631
online.dll
 
Thank you Chad, it works exactly like I want it to. perfect!
Now if you have the time can you give me a quick break down of why your code works and mine does not. This is my 4th day of real coding, so I'm still a little confused.
Thanks
Robert
 
Ok it is clear to me that I must be missing something, I just tried to add a second line of code to grab and echo line 116. so now I would be echoing line 103 and line 116. but the code only echo's line 103. why?

also should I be ending the connection from my server to theirs, or does that happen automatically.
Thank again
Robert

Code:
<?php 
/* 
overview of code.
To submit info to a website via its URL, utilize variables from the POST form eagle5.htm which are concated into the url.
*/ 
/*read variables and get site info*/

$filename = &quot;[URL unfurl="true"]http://www.geocode.com/scripts/eagle/eagle.pl?dummy=dummy&street=&quot;.urlencode($street).&quot;&city=&quot;.urlencode($city).&quot;&state=&quot;.urlencode($state).&quot;&zip=&quot;.urlencode($zip).&quot;&geocode=request&request=Address+Info&cmd=td_g&dum2=dummy/&quot;;[/URL]

$fc = @file($filename);

$full_contents = @implode(&quot;&quot;,$fc);
/*looping through html of site to find line 103 then echo line*/
while (list($line, $content)=each($fc)) {
     if($line == 103) {
          echo $content;
     }
}
/*looping through html of site to find line 116 then echo line*/
while (list($line, $content)=each($fc)) {
     if($line == 116) {
          echo $content;
     }
}

?>
 
When you do file($filename), you are creating an array which each line of the file being an element of the array. Then you are using the while() loop to list each element of the array.

At that point, when the loop is done listing the array, your internal array pointer is now at the end of the array, so you need to reset your array, if you want to loop through it again. Just do
Code:
reset($fc);
before starting your next while() loop. (
But really, there is no need to loop through your array twice. Just do:

Code:
while (list($line, $content)=each($fc)) {
     if($line == 103) {
          echo $content;
     }

     if($line == 116) {
          echo $content;
     }
}

Now, I'm doing my best to be instructive, while correcting code, so that's why I haven't jumped straight to the simplest thing, which is that the above can also be accomplished by:

Code:
<?php

$filename = &quot;[URL unfurl="true"]http://www.geocode.com/scripts/eagle/eagle.pl?dummy=dummy&street=&quot;.urlencode($street).&quot;&city=&quot;.urlencode($city).&quot;&state=&quot;.urlencode($state).&quot;&zip=&quot;.urlencode($zip).&quot;&geocode=request&request=Address+Info&cmd=td_g&dum2=dummy/&quot;;[/URL]

$fc = @file($filename);

echo $fc[103];

echo $fc[116];

?>

Of course, your loop also checks for the existence of the line before printing it, which the latter does not do, but that can be accomplished by &quot;if(isset($fc[116])) { echo $fc[116]; }&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top