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
https://forums.phpfreaks.com/topic/59765-php-web-fetching/#findComment-297094
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
https://forums.phpfreaks.com/topic/59765-php-web-fetching/#findComment-297123
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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