php socket programming with auth - php

i want to read file from remote server with Socket programing but server protected with http_auth
and i could not get socket working even with providing tcp://username:passwrod#hostname.com
i got this error
php_network_getaddresses: getaddrinfo failed: No such host is known. (0)
here is my code
$fp = stream_socket_client(" tcp://username:passwrod#hostname.com:80", $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\nHost: hostname.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}

Well, for one, you have a typo in the TCP string:
" tcp://username:passwrod#hostname.com:80"
should be
"tcp://username:password#hostname.com:80"
By itself, the extra space at the start of your URI will cause yourstream_socket_client call to fail, and I imagine it's responsible for the getaddrinfo error as the host cannot be resolved if the URI string is invalid. Furthermore, even once the DNS issue is resolved, the authentication will fail because password is misspelled as passwrod.
Assuming it still doesn't work after that, you should have a look at your DNS settings. getaddrinfo failed: No such host is known means that your server cannot figure out where hostname.com is located... so before you start worrying about authentication or ports, just make sure that
hostname.com is a valid URL
the DNS service is configured correctly on your server (try to run a few simple ones that should definitely work, like $fp = stream_socket_client("tcp://yahoo.com:80", $errno, $errstr, 30); var_dump($fp); - if that call doesn't return a resource then your DNS service is not functioning correctly... or you could just try pinging from the server to see if you can reach www.yahoo.com.
Check your server's proxy settings

Try this:
<?php
if(!($fp = fsockopen("hostname", 80)))
{
throw new Exception("Couldn't open socket!\n");
}
fputs($fp, "GET / HTTP/1.0\r\n");
fputs($fp, "Host: hostname\r\n");
fputs($fp, "Authorization: Basic " .
base64_encode("username:password") .
"\r\n");
fputs($fp, "\r\n");
fpassthru($fp);
?>

Related

How can I receive data from GPS device which is sending data to my server ip and port using PHP?

I have a GPS device which is sending data to my server 103.209.146.80 and port 5672
I want to create a php script or a JS script which reads the incoming data on this port.
My first step is to catch the data. I am using this PHP code but it is giving error:
<?php
$fp = fsockopen("103.209.146.80", 5672, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "You message");
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
You must first know what type of connection tcp or udp transmits your GPS, then create a socket server (tcp / udp) that listens on the port where you will transmit your GPS.
Do not forget that you must configure a valid APN in your GPS.

Check whether outbound port on server is open

I am trying to see whether my port 2195 (apples port) is open for outbound executions. I know that it is closed for inbound. I want to be able to check using php so I wrote this code:
$fp = fsockopen("example.co.uk", 2195, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fclose($fp);
}
Which returns the error:
Warning: fsockopen(): unable to connect to example.co.uk:2195
(Connection refused) in /home3/.../public_html/.../example.php
on line 13 Connection refused (111)
But as I said I think this is due to the inbound port being closed.
So is there another way to test just the outbound?

Where the headers come from when just calling echo in PHP?

I see come code from PHP manual
$fp = fsockopen("www.example.com", 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);
}
writing header by echo, is this going to work?
when just calling simple code like this:
echo 'hello';
where the http headers from?
What you're doing in this code is:
Open a socket connection to a remote HTTP server (www.example.com on port 80). This establishes a TCP connection to that port.
You then send (via fwrite) an HTTP request over this connection. HTTP is a protocol on top of TCP, and you're manually formulating your HTTP protocol headers here.
You're then reading (via fgets) the (HTTP) response of the remote server.
I'm guessing that you want to know why you see HTTP headers in this remote response, even though you're only doing echo 'hello'; on the remote server. The answer is because the web server running on that server is handling the HTTP transaction. You're not handling any details of the incoming HTTP request in PHP, and you're also not handling any details of the outgoing response. The web server on which PHP is running (likely Apache) is doing that.
The whole stack includes a TCP connection, which carries an HTTP request, which consists of HTTP headers and an HTTP body. On the server, the TCP connection is typically handled by the underlying operating system, which makes the connection available as a socket to the web server, where the web server "unwraps" the HTTP request to handle it and invoke PHP if necessary, and then the whole chain goes backwards for the response.

I can't use Paypal IPN on my Hosting, how to solve this?

I'm testing paypal ipn for a website I am building, but it fails when I try to validate the ipn. In particular this line doesn't work:
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
the check if (!$fp) fails (fp is false). I assume it can't connect. Why this?
Update
I discovered my hosting solution doesn't support ssl, in fact I get:
Warning: fsockopen() [function.fsockopen]: unable to connect to
ssl://www.sandbox.paypal.com:443 (Unable to find the socket transport
"ssl" - did you forget to enable it when you configured PHP?)
Is there a way to get over this without going for another hosting solution?
You don't have to use SSL (though Paypal highly recommends it.)
$fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);

PHP fwrite() to socket - operation not permitted?

I'm making a simple utility in PHP to control my Minecraft server via UDP:
$fp = fsockopen('udp://host', 'port', $errno, $errstr);
if (!$fp)
error("Unable to connect!");
else {
fwrite($fp, $data['command'].':user:pass');
stream_set_timeout($fp, 5);
error(fread($fp, 128));
fclose($fp);
}
For some reason fwrite is throwing this error:
Notice: fwrite() [function.fwrite]: send of 20 bytes failed with errno=1 Operation not permitted in /homepages/44/d217581656/htdocs/xenforo/util/remoterestart/interface.php on line 22
Anyone know why?
I'm going to go out on a limb and say you're probably not permitted to use sockets on your server. This is a fairly common thing to disable on most hosts.
Run a phpinfo() and see if there is any socket functions which are disabled, or contact your host and just ask.

Categories