Jump to content

PHP web fetching ?


jd2007

Recommended Posts

fetching? as in getting data off a url? try fopen or cURL or socket type connection if you are trying to get info off an outside server

fopen(opens a doc post processing and you get the doc's entire length (tricky,copy right infractions, but can be useful at times)

cURL(sends info off to a processor type page and returns with info back. (good for like stock look ups)

socket(makes a connection to a server and can extract data from flat files (paypal uses this for IPN in which data is stored in flat files for cross language use

Link to comment
Share on other sites

There really is no need for anything like snoopy.  You can create your own sockets or curl connections fairly simply.

 

Try something like:

 

<?php
function getPageData($pageurl) {
     $_url  = parse_url($pageurl);
     $port = strtolower($_url['scheme']) == 'https' ? 443 : 80;
     $_sck = fsockopen($_url['host'], $port, $errNo, $errStr);
     if (!$_sck)
     {
           die("Socket error [#$errNo]: $errStr");
     }
     $path = $_url['path'] . '?' . $_url['query'];
     $request = "GET $path HTTP/1.1\r\n";
     $request .= ($port == 443) ? "Host: {$_url['host']}\r\n";
     $request .= "Connection: close\r\n\r\n";
     fputs($_sck, $request, strlen($request));
     $response = '';
     while (!feof($_sck))
     {
          $response .= fgets($_sck, 1024);
     }
     echo $response;
}
getPageData('http://google.com/index.html');
?>

Tested, works, and a hell of a lot smaller than snoopy.  Granted it doesn't have the features, but if all you need is to pull data from a website...there you go.  Actually, there's also a much much easier way:

 

<?php
print implode("\n", file("http://google.com/index.html"));
?>

But that won't always work.  You shouldn't have a problem with sockets.

Link to comment
Share on other sites

You can even use file_get_contents for simple GET requests!

 

<?php
       print file_get_contents("http://www.google.com");
?>

Yes, but it's PHP5 only, which is why I did the file() example in mine...backwards compatibility.
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.