Jump to content

php sockets?


acidglitter

Recommended Posts

Here's a simple example that'll pull information off of a web page

<?php
function getHTML($website) {
    $url = parse_url($website);
    $handle = fsockopen($url['host'], 80);
    $request = "GET {$url['path']} HTTP/1.1\r\n";
    $request .= "Host: {$url['host']}\r\n\r\n";
    fputs($handle, $request, strlen($request));
    $response = "";
    while (!feof($handle)) $response .= fgets($handle, 1024);
    print $response;
}
?>

That should work, then you can manipulate it as you feel needed

Link to comment
Share on other sites

Thanks  ;D

 

I'm not really sure how to use it though. I put

getHTML("http://vanillaeyeliner.com");

 

but I got this error

 

Bad Request

Your browser sent a request that this server could not understand.

 

Invalid URI in request GET HTTP/1.1

 

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/1.3.37 Server at x.geminiserver.com Port 80

Link to comment
Share on other sites

You must call the function and give it the variable.

 

<?php
function getHTML($website) {
    $url = parse_url($website);
    $handle = fsockopen($url['host'], 80);
    $request = "GET {$url['path']} HTTP/1.1\r\n";
    $request .= "Host: {$url['host']}\r\n\r\n";
    fputs($handle, $request, strlen($request));
    $response = "";
    while (!feof($handle)) $response .= fgets($handle, 1024);
    print $response;
}
?>

 

$website = 'http://www.google.com'
getHTML($website);

Link to comment
Share on other sites

I forgot to add something in the code.  Try it now.

<?php
function getHTML($website) {
    $url = parse_url($website
    $url['path'] = substr($url['path'], 0, 1) == "/" ? $url['path'] : "/" . $url['path'];
    $handle = fsockopen($url['host'], 80);
    $request = "GET {$url['path']} HTTP/1.1\r\n";
    $request .= "Host: {$url['host']}\r\n\r\n";
    fputs($handle, $request, strlen($request));
    $response = "";
    while (!feof($handle)) $response .= fgets($handle, 1024);
    print $response;
}
?>

Link to comment
Share on other sites

Thats awesome. Thanks ;D ;D

 

So my code looks like this:

<?php
function getHTML($website) {
    $url = parse_url($website);
    $url['path'] = substr($url['path'], 0, 1) == "/" ? $url['path'] : "/" . $url['path'];
    $handle = fsockopen($url['host'], 80);
    $request = "GET {$url['path']} HTTP/1.1\r\n";
    $request .= "Host: {$url['host']}\r\n\r\n";
    fputs($handle, $request, strlen($request));
    $response = "";
    while (!feof($handle)) $response .= fgets($handle, 1024);
    print $response;
}

$website = 'http://www.google.com';
getHTML($website);

?>

 

How should I change it so it still gets the entire page, but then the only thing it displays is the Google logo image?

Link to comment
Share on other sites

function getHTML($website) {
    $url = parse_url($website);
    $url['path'] = substr($url['path'], 0, 1) == "/" ? $url['path'] : "/" . $url['path'];
    $handle = fsockopen($url['host'], 80);
    $request = "GET {$url['path']} HTTP/1.1\r\n";
    $request .= "Host: {$url['host']}\r\n\r\n";
    fputs($handle, $request, strlen($request));
    $response = "";
    while (!feof($handle)) $response .= fgets($handle, 1024);
    print strip_tags($response,'<img>');
}

Link to comment
Share on other sites

So it showed this

<img alt='' height=1 id=gbari width=1>document.getElementById('gbarc').style.height=(document.getElementById('gbari').height=window.gbar.getPad())+'px';document.getElementById('gbar').innerHTML=window.gbar.getHtml([['w','Web','',1],['i','Images','http://images.google.com/imghp',1],['v','Video','http://video.google.com/',1],['n','News','http://news.google.com/nwshp',1],['l','Maps','http://maps.google.com/maps',1],['m','Gmail','http://mail.google.com/mail',1],['','more','#',3],['b','Blog Search','http://blogsearch.google.com/',2],['j','Blogger','http://www.blogger.com/',2],['p','Books','http://books.google.com/bkshp',2],['c','Calendar','http://www.google.com/calendar',2],['o','Documents','http://docs.google.com/',2],['e','Finance','http://finance.google.com/finance',2],['g','Groups','http://groups.google.com/grphp',2],['z','Labs','http://labs.google.com/',2],['0','Orkut','http://www.orkut.com/',2],['t','Patents','http://www.google.com/ptshp',2],['q','Photos','http://picasaweb.google.com/home',2],['f','Products','http://www.google.com/prdhp',2],['y','Reader','http://www.google.com/reader',2],['s','Scholar','http://scholar.google.com/schhp',2]]);iGoogle | Sign in<img alt="Google" height=110 src="/intl/en_ALL/images/logo.gif" width=276>   Advanced Search  Preferences  Language ToolsAdvertising Programs - Business Solutions - About Google©2007 Google

 

I meant more of how could I get it to get the exact address of the image, like only show this

intl/en_ALL/images/logo.gif
Link to comment
Share on other sites

<?php
function getImages($website) {
    $url = parse_url($website);
    $url['path'] = substr($url['path'], 0, 1) == "/" ? $url['path'] : "/" . $url['path'];
    $handle = fsockopen($url['host'], 80);
    $request = "GET {$url['path']} HTTP/1.1\r\n";
    $request .= "Host: {$url['host']}\r\n\r\n";
    fputs($handle, $request, strlen($request));
    $response = "";
    while (!feof($handle)) $response .= fgets($handle, 1024);
    $response = strip_tags($response,'<img>');
    preg_match_all("@<img[^>]+?src=\"([^\"]+)\"[^>]+?>@is", $response, $matches);
    return $matches;
}
print_r(getImages("http://www.google.com"));
?>

Link to comment
Share on other sites

Thanks for all the help so far ;D

This is probably my last question about it..

 

If I wanted to see what my myspace inbox looked like, using the original code it would look like this..

<?php
function getHTML($website) {
    $url = parse_url($website);
    $url['path'] = substr($url['path'], 0, 1) == "/" ? $url['path'] : "/" . $url['path'];
    $handle = fsockopen($url['host'], 80);
    $request = "GET {$url['path']} HTTP/1.1\r\n";
    $request .= "Host: {$url['host']}\r\n\r\n";
    fputs($handle, $request, strlen($request));
    $response = "";
    while (!feof($handle)) $response .= fgets($handle, 1024);
    print $response;
}

$website = 'http://messaging.myspace.com/index.cfm?fuseaction=mail.inbox';
getHTML($website);

?>

 

But the problem is I need to be signed in to see it.. Is there a way I could put my username/password in the socket so it could still get the page html?

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.