PHP gurus - help needed here

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • who
    So Fucking Banned
    • Aug 2003
    • 19593

    #1

    PHP gurus - help needed here

    I'm trying to find the function that will help me extract the HTML from a page located in another frame (on different domain):

    In PERL it would be something like:

    Code:
    get('http://www.site.com/this.htm');
    What's something equivalent to this in PHP?
  • Varius
    Confirmed User
    • Jun 2004
    • 6890

    #2
    You can use the function fopen, or you could also call the page (esp. if you need to post variables to it) by using the curl functions
    Skype variuscr - Email varius AT gmail

    Comment

    • who
      So Fucking Banned
      • Aug 2003
      • 19593

      #3
      Originally posted by Varius
      You can use the function fopen, or you could also call the page (esp. if you need to post variables to it) by using the curl functions
      OK, thanks. I'll look into that. big help

      Comment

      • Varius
        Confirmed User
        • Jun 2004
        • 6890

        #4
        Originally posted by .?.
        OK, thanks. I'll look into that. big help
        You should be able to find both and their format easily on php.net btw
        Skype variuscr - Email varius AT gmail

        Comment

        • VideoJ
          Confirmed User
          • Aug 2002
          • 750

          #5
          See also file - http://es.php.net/manual/en/function.file.php and file_get_contents http://es.php.net/manual/en/function...t-contents.php
          Somebody stole my damn signture...

          Comment

          • who
            So Fucking Banned
            • Aug 2003
            • 19593

            #6
            Hmm, I thought I had it, but this doesn't work. If someone knows what would make this work, please let me know.
            Code:
            <?
            
            $key = "this text";
            $fc=file("http://www.site.com/page.htm");
            $f=fopen("test.html","w");
            foreach($fc as $line)
            {
               if (!strstr($line,$key))
            	 str_replace('$key','replaced text',$line);
                       fputs($f,$line); 
            		echo $line;
            }
            fclose($f);
            
            ?>

            Comment

            • who
              So Fucking Banned
              • Aug 2003
              • 19593

              #7
              C'mon pimps, there's gotta be some real webmasters here?

              Comment

              • Repetitive Monkey
                Confirmed User
                • Feb 2004
                • 3505

                #8
                <?
                $url="http://www.the-url.com/";
                $parsed=parse_url($url);
                $socket=@fsockopen($parsed[host],80,&$errno,&$errstr,10);
                if($socket){
                @socket_set_timeout($socket,10);
                fputs($socket,"GET ".$url." HTTP/1.0\r\nHost: ".$parsed[host]."\r\nReferer: http://www.someone-else.com/\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\n\r\n");
                while(!feof($socket)){
                $html.=fgets($socket,1024);
                }
                fclose($socket);
                if($html){
                $html=explode("\r\n\r\n",$html);
                unset($html[0]);
                $html=implode("\r\n\r\n",$html);
                echo $html;
                }else{
                echo"No HTML was returned.";
                }
                }else{
                echo"Socket couldn't be opened.";
                }
                ?>

                Comment

                • who
                  So Fucking Banned
                  • Aug 2003
                  • 19593

                  #9
                  Thanks Repetitive Monkey. It should help. My next step is trying to replace a certain word with another certain word, and save that html to a file.

                  Comment

                  • Repetitive Monkey
                    Confirmed User
                    • Feb 2004
                    • 3505

                    #10
                    Originally posted by .?.
                    Thanks Repetitive Monkey. It should help. My next step is trying to replace a certain word with another certain word, and save that html to a file.
                    Replace "echo $html;" in my previous code with the following code:

                    $html=str_replace("word","newword",$html);
                    $save=@fopen("somefile.txt",w);
                    if($save){
                    if(@fputs($save,$html)){
                    echo"HTML saved to somefile.txt";
                    }else{
                    echo"HTML couldn't be saved to somefile.txt";
                    }
                    @fclose($save);
                    }else{
                    echo"A writable connection with somefile.txt couldn't be established.";
                    }

                    Comment

                    • who
                      So Fucking Banned
                      • Aug 2003
                      • 19593

                      #11
                      Originally posted by Repetitive Monkey
                      Replace "echo $html;" in my previous code with the following code:

                      $html=str_replace("word","newword",$html);
                      $save=@fopen("somefile.txt",w);
                      if($save){
                      if(@fputs($save,$html)){
                      echo"HTML saved to somefile.txt";
                      }else{
                      echo"HTML couldn't be saved to somefile.txt";
                      }
                      @fclose($save);
                      }else{
                      echo"A writable connection with somefile.txt couldn't be established.";
                      }
                      Thank you very much. I hope you didn't write that just for me, or that if you did, it only took you a few seconds

                      Thank you again for the help. Proved to me that GFY isn't all bad...

                      Comment

                      • Repetitive Monkey
                        Confirmed User
                        • Feb 2004
                        • 3505

                        #12
                        Originally posted by .?.
                        Thank you very much. I hope you didn't write that just for me, or that if you did, it only took you a few seconds

                        Thank you again for the help. Proved to me that GFY isn't all bad...
                        I did, but it was quick, yes.

                        The tabs formatting the code were swallowed by the GFY monster, so if you want it to look neat I can send you the txtfile through e-mail.

                        Comment

                        • Repetitive Monkey
                          Confirmed User
                          • Feb 2004
                          • 3505

                          #13
                          Nevermind, the code function delivers the tabs.

                          Code:
                          <?
                          $url="http://www.the-url.com/";
                          $parsed=parse_url($url);
                          $socket=@fsockopen($parsed[host],80,&$errno,&$errstr,10);
                          if($socket){
                          @socket_set_timeout($socket,10);
                          fputs($socket,"GET ".$url." HTTP/1.0\r\nHost: ".$parsed[host]."\r\nReferer: http://www.someone-else.com/\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\n\r\n");
                          	while(!feof($socket)){
                          	$html.=fgets($socket,1024);
                          	}
                          fclose($socket);
                          	if($html){
                          	$html=explode("\r\n\r\n",$html);
                          	unset($html[0]);
                          	$html=implode("\r\n\r\n",$html);
                          	$html=str_replace("word","newword",$html);
                          	$save=@fopen("somefile.txt",w);
                          		if($save){
                          			if(@fputs($save,$html)){
                          			echo"HTML saved to somefile.txt";
                          			}else{
                          			echo"HTML couldn't be saved to somefile.txt";
                          			}
                          		@fclose($save);
                          		}else{
                          		echo"A writable connection with somefile.txt couldn't be established.";
                          		}
                          	}else{
                          	echo"No HTML was returned.";
                          	}
                          }else{
                          echo"Socket couldn't be opened.";
                          }
                          ?>

                          Comment

                          • who
                            So Fucking Banned
                            • Aug 2003
                            • 19593

                            #14
                            Thanks again, and it works beautifully!

                            Comment

                            • xfalmp
                              Confirmed User
                              • Aug 2002
                              • 1474

                              #15
                              A smaller version.

                              <?
                              $url = "http://www.google.com";
                              $file = "google.htm";
                              $string = "href="; // search for
                              $newstring = "target='_blank' href="; // replace with

                              $html = file_get_contents($url);
                              $html = str_replace($string,$newstring,$html);
                              file_put_contents($file,$html);
                              ?>

                              Trust the "WOW" effect.

                              Comment

                              • who
                                So Fucking Banned
                                • Aug 2003
                                • 19593

                                #16
                                Originally posted by xfalmp
                                A smaller version.

                                <?
                                $url = "http://www.google.com";
                                $file = "google.htm";
                                $string = "href="; // search for
                                $newstring = "target='_blank' href="; // replace with

                                $html = file_get_contents($url);
                                $html = str_replace($string,$newstring,$html);
                                file_put_contents($file,$html);
                                ?>
                                Nice. This is what I was trying to come up with, but RM's version has some nice annonymity thrown in, and looks safe, and is just bloody amazing to read (for a php nub like me).

                                Comment

                                • xfalmp
                                  Confirmed User
                                  • Aug 2002
                                  • 1474

                                  #17
                                  Hey, to each his own.. I don't see why use sockets though.

                                  Anyway, just showing you there's more than one way to do, just like PERL.

                                  Trust the "WOW" effect.

                                  Comment

                                  • Repetitive Monkey
                                    Confirmed User
                                    • Feb 2004
                                    • 3505

                                    #18
                                    Originally posted by xfalmp
                                    Hey, to each his own.. I don't see why use sockets though.
                                    The reason for not using just a string of premade functions is to have more control of what the script is doing, the way it behaves under different conditions, and its output.

                                    Comment

                                    Working...