Use fsockopen with proxy - php

I have a simple whois script
if($conn = fsockopen ($whois_server, 43)) {
fputs($conn, $domain."\r\n");
while(!feof($conn)) {
$output .= fgets($conn, 128);
}
fclose($conn);
return $output;
}
$whois_server = whois.afilias.info; //whois server for info domains
but I want to run in through proxy. So I need to connect to proxy -> then connect to whois server -> and then make the request. Something like this?
$fp = fsockopen($ip,$port);
fputs($fp, "CONNECT $whois_server:43\r\n $domain\r\n");
But it doesn't work, I don't know if i'm making the second connection right.

Sending your request to the proxy should do what you like:
<?php
$proxy = "1.2.3.4"; // proxy
$port = 8080; // proxy port
$fp = fsockopen($proxy,$port); // connect to proxy
fputs($fp, "CONNECT $whois_server:43\r\n $domain\r\n");
$data="";
while (!feof($fp)) $data.=fgets($fp,1024);
fclose($fp);
var_dump($data);
?>
Rather than doing that I would recommend you using CURL in combination with:
curl_setopt($ch, CURLOPT_PROXY, 1.2.3.4);

Related

Writing and Reading from a socket a second time

I connect to the whois server and am able to retrieve the availability of a domain name.
Somehow I am not able to get a response back using the same connection when I do a request of a different domain name.
<?php
$context = stream_context_create();
if($fp = stream_socket_client("tcp://whois.eu:43", $errno, $errstr, 30, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $context)) {
stream_set_timeout($fp, 30);
$domains = array('test.eu','amaai.eu');
foreach($domains as $domain) {
fwrite($fp, $domain."\r\n");
$contents = '';
while (!feof($fp)) {
$contents .= fread($fp, 8192);
}
echo $domain.": ".$contents;
}
fclose($fp);
}
What am I missing?
I really want to use the same connection.
The WHOIS protocol only supports one query. The server closes the connection after sending a response. You need to reconnect for each query.

PHP fsockopen error, connection refused(111), try to connect fingerprint device using TCP/IP

i was want to connect to my fingerprint device useing TCP/IP
i was setting ip address in device,
my php code:
<?php
$fp = fsockopen("192.168.1.211", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
when i try to connect, i get error connection refused . but ping to device is work, i don't know where is the problem,
My fingerprint device specification:
Vendor :Solution
type : x101-c
i hope somebody can help and solve my problem,,
i really stack now....
but ping to device is work
Ping uses ICMP packets not TCP. If you're getting a connection refused then the (TCP) port is firewalled.

Use Proxy With fsockopen

I have a Pagerank checking script that i'd like to add proxies to. I usually use Curl so im not sure exactly how to go about making the request through a proxy. I want the request to pick a random proxy from the $proxies array and use that for the request. Can anyone walk me through how this is done using this script. Thanks in advance.
$proxyauth = "username:password";
$proxies = array(
"proxy1",
"proxy2",
"proxy3",
"proxy4",
"proxy5",
"proxy6",
"proxy7",
"proxy8",
"proxy9",
"proxy10"
);
function check($page){
// Open a socket to the toolbarqueries address, used by Google Toolbar
$socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
// If a connection can be established
if($socket) {
// Prep socket headers
$out = "GET /tbr?client=navclient-auto&ch=".$this->checkHash($this->createHash($page))
."&features=Rank&q=info:".$page."&num=100&filter=0 HTTP/1.1\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
$out .= "Connection: Close\r\n\r\n";
// Write settings to the socket
fwrite($socket, $out);
// When a response is received...
$result = "";
while(!feof($socket)) {
$data = fgets($socket, 128);
$pos = strpos($data, "Rank_");
if($pos !== false){
$pagerank = substr($data, $pos + 9);
$result += $pagerank;
}
}
// Close the connection
fclose($socket);
// Return the rank!
return $result;
}
}
With fsockopen, instead of toolbarqueries.google.com you connect to the selected proxy. Then you send a complete URL with the GET request:
$socket = fsockopen("proxy5", 80, $errno, $errstr, 30);
...
$out = "GET http://toolbarqueries.google.com/tbr?client=..."
You can still send the Host header, but you don't need it.

Fsockopen and proxy authentication in PHP

I'm trying to integrate proxy usage (with authentication) into a script that queries whois data.
What I'm trying to do is
1) Connect to the proxy IP and port
2) authenticate a username and password
3) connect to the whois server and send domain details, receiving the request in return.
I have the script working without proxies
private function whois($domeinnaam, $whoisrule)
{
list ($server, $poort, $domein, $vrij) = $whoisrule;
$domein = str_replace("{domein}", $domeinnaam, $domein);
$fp = fsockopen($server, $poort);
if($fp)
{
fputs($fp, $domein."\r\n");
$data = "";
while(!feof($fp))
{
$data .= fread($fp, 1000);
}
fclose($fp);
}
else
{
$data = "error";
}
// Cache whois data
$this->_whoisdata[$domein] = $data;
return $data;
}
But does anyone how I would integrate a proxy server and authentication into this code?
cURL has some handy CURLOPT_PROXY* options. This answer shows how to use them.

How do you get the HTTP status code for a remote domain in php?

I would like to create a batch script, to go through 20,000 links in a DB, and weed out all the 404s and such. How would I get the HTTP status code for a remote url?
Preferably not using curl, since I dont have it installed.
CURL would be perfect but since you don't have it, you'll have to get down and dirty with sockets. The technique is:
Open a socket to the server.
Send an HTTP HEAD request.
Parse the response.
Here is a quick example:
<?php
$url = parse_url('http://www.example.com/index.html');
$host = $url['host'];
$port = $url['port'];
$path = $url['path'];
$query = $url['query'];
if(!$port)
$port = 80;
$request = "HEAD $path?$query HTTP/1.1\r\n"
."Host: $host\r\n"
."Connection: close\r\n"
."\r\n";
$address = gethostbyname($host);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $address, $port);
socket_write($socket, $request, strlen($request));
$response = split(' ', socket_read($socket, 1024));
print "<p>Response: ". $response[1] ."</p>\r\n";
socket_close($socket);
?>
UPDATE: I've added a few lines to parse the URL
If im not mistaken none of the php built-in functions return the http status of a remote url, so the best option would be to use sockets to open a connection to the server, send a request and parse the response status:
pseudo code:
parse url => $host, $port, $path
$http_request = "GET $path HTTP/1.0\nHhost: $host\n\n";
$fp = fsockopen($host, $port, $errno, $errstr, $timeout), check for any errors
fwrite($fp, $request)
while (!feof($fp)) {
$headers .= fgets($fp, 4096);
$status = <parse $headers >
if (<status read>)
break;
}
fclose($fp)
Another option is to use an already build http client class in php that can return the headers without fetching the full page content, there should be a few open source classes available on the net...
This page looks like it has a pretty good setup to download a page using either curl or fsockopen, and can get the HTTP headers using either method (which is what you want, really).
After using that method, you'd want to check $output['info']['http_code'] to get the data you want.
Hope that helps.
You can use PEAR's HTTP::head function.
http://pear.php.net/manual/en/package.http.http.head.php

Categories