I attempting to connect to a server via PHP fsockopen to initially get a cookie for basic auth and then to persistently connect to a streaming server defined in the Location header of the response.
The problem is that my code freezes on fgets and never receives any response data from the destination server. I'm connecting via https on port 443 on an Amazon ec2 instance. The server connects fine via curl in my server's terminal or via my chome browser.
$this->conn = fsockopen('ssl://[destination_server]', 443, $errNo, $errStr, $this->connectTimeout);
stream_set_blocking($this->conn, 1);
fwrite($this->conn, "GET " . $urlParts['path'] . " HTTP/1.0\r\n");
fwrite($this->conn, "Host: " . $urlParts['host'] . "\r\n");
fwrite($this->conn, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($this->conn, "Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
fwrite($this->conn, 'Authorization: Basic ' . $authCredentials . "\r\n");
fwrite($this->conn, 'User-Agent: ' . self::USER_AGENT . "\r\n");
list($httpVer, $httpCode, $httpMessage) = preg_split('/\s+/', trim(fgets($this->conn, 1024)), 3);
//code never gets here!!!
Any thoughts?
I solved this problem by adding the header: "Connection: Close\r\n\r\n".
The initial request for the cookie returns a 302 redirect code and will sit on the open connection unless you pass that header.
Unfortunately, this little line had me stumped for a while.
Related
I am trying to send data to PHP websocket server, although it sends the data but the data received is a garbage values. How can fix this to get correct values posted to websocket php server?
Below is my websocket php client script
<?php
$host = 'example.com:9000/server.php'; //where is the websocket server
$port = 9000; //ssl
$local = "http://localhost/"; //url where this script run
$data = json_encode(array("server_msg"=> "1","device_id"=> "DDD-123455678")); //data to be send
$head = "GET / HTTP/1.1"."\r\n".
"Host: $host"."\r\n".
"Upgrade: websocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."\r\n".
"Sec-WebSocket-Version: 13"."\r\n".
"Content-Length: ".strlen($data)."\r\n"."\r\n";
////WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000); //receives the data included in the websocket package "\x00DATA\xff"
$retdata = trim($wsdata,"\x00\xff"); //extracts data
////WebSocket handshake
fclose($sock);
echo $retdata;
?>
Thanks
Hi,
I have already tried it and it gives me error as below:
Fatal error: Uncaught exception 'WebSocket\ConnectionException' with message 'Connection to 'ws://************/server.php' in /var/www/webclientphp/vendor/textalk/websocket/lib/Client.php on line 149
WebSocket\ConnectionException: Connection to 'ws://************/server.php' failed: Server sent invalid upgrade response: HTTP/1.1 101 Web Socket Protocol Handshake Upgrade: websocket Connection: Upgrade WebSocket-Origin: ************ WebSocket-Location: ws://************:9000/demo/shout.php Sec-WebSocket-Accept:Kfh9QIsMVZcl6xEPYxPHzW8SZ8w= in /var/www/webclientphp/vendor/textalk/websocket/lib/Client.php on line 149
Please help
Your data needs to be encoded to match the Websocket protocol (frames, headers, encryption etc).
The server will be expecting websocket frames, and will try to decode them as per the protocol, so you can't just send raw data. It will also send data to you in this format.
The easiest way is to use a library, like this one
I have a PHP Class to push message with socket like :
function __construct()
{
$this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 0); //I tried with 99999 for timeout too
}
function push($params)
{
$req = 'GET /push?'.$params." HTTP/1.1\r\n"
. 'Host: '.$this->host."\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. 'Content-Length: '.strlen($params)."\r\n"
. "Connection: keep-alive\r\n\r\n";
fwrite($this->socket, $req);
}
But if I tried to push 2 or more message, only one is receive by NodeJS serveur :
foreach(['foo=2', 'bar=42'] as $loop)
{
$pusher->push($loop);
}
Now, if i put this line $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 0); just before fwrite, all messages will be send...
So why I can't use only one connexion for multiple request with same soket?
I use "keep-alive" and I don't call fclose, so I don't understand why my nodeJS server receive only one message in first case...
I am trying to figure out if there is a way to do a curl post, but without receiving the response.
I know I can prevent the response from being displayed by setting CURLOPT_RETURNTRANSFER to false, but I don't even want to receive it.
I am working on a proxy and need to do the post request to a webservice, but the response it gives is MASSIVE and ends up timing out my connection (even when set to 200 seconds).
Even if it did work, that's just way too long since I don't care what the response is at all.
I can't seem to find a way to do this.
Try using a socket rather than using cURL:
$requestBody = 'myparams=something&something=somethingelse';
$socket = fsockopen("mysite.com", 80, $errno, $errstr, 15);
if (!$socket) {
// Handle the error, can't connect
} else {
$http = "POST /path/to/post/to HTTP/1.1\r\n";
$http .= "Host: mysite.com\r\n";
$http .= "Content-Type: application/x-www-form-urlencoded\r\n";
$http .= "Content-length: " . strlen($post_data) . "\r\n";
$http .= "Connection: close\r\n\r\n";
$http .= $requestBody . "\r\n\r\n";
fwrite($socket, $http);
fclose($socket);
}
This will submit the POST request and not wait around for the response.
I have a working websocket Server (python + Tornado) which accepts Connections on port 8973. I can connect by a easy JavaScript / jquery instruction like:
ws = new WebSocket("ws://192.168.41.170:8973/rt");
But I need my php script to connect to this websocket server and send a message. I tried all most available solutions like
https://github.com/lemmingzshadow/php-websocket/
$host = '192.168.41.170'; //where is the websocket server
$port = 8973;
$local = "http://192.168.41.2/"; //url where this script run
$data = 'hello world!'; //data to be send
$head = "GET / HTTP/1.1"."\r\n".
"Upgrade: WebSocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Origin: $local"."\r\n".
"Host: $host"."\r\n".
"Content-Length: ".strlen($data)."\r\n"."\r\n";
//WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000); //receives the data included in the websocket package "\x00DATA\xff"
fclose($sock);
But this all dont work. Has anybody a working code snippet? I dont need a php-websocket server! Thanks
I think your problem is in how you are packing the data. Without knowing which protocol you are trying to use, I can't tell you how to pack it! But, there is a working code snippet in one answer to this question.
I am trying to send a SOAP message to a service using php.
I want to do it with fsockopen, here's is the code :
<?php
$fp = #fsockopen("ssl://xmlpropp.worldspan.com", 443, $errno, $errstr);
if (!is_resource($fp)) {
die('fsockopen call failed with error number ' . $errno . '.' . $errstr);
}
$soap_out = "POST /xmlts HTTP/1.1\r\n";
$soap_out .= "Host: 212.127.18.11:8800\r\n";
//$soap_out .= "User-Agent: MySOAPisOKGuys \r\n";
$soap_out .= "Content-Type: text/xml; charset='utf-8'\r\n";
$soap_out .= "Content-Length: 999\r\n\r\n";
$soap_put .= "Connection: close\r\n";
$soap_out .= "SOAPAction:\r\n";
$soap_out .= '
Worldspan
This is a test
';
if(!fputs($fp, $soap_out, strlen($soap_out)))
echo "could not write";
echo "<xmp>".$soap_out."</xmp>";
echo "--------------------<br>";
while (!feof($fp))
{
$soap_in .= fgets($fp, 100);
}
echo "<xmp>$soap_in</xmp>";
fclose($fp);
echo "ok";
the above code just hangs . if i remove the while it types ok, so i suppose the problem is at $soap_in .= fgets($fp, 100)
Any ideas of what is happening
Its not just a matter of opening a socket then writing 'POST....' to it - you need a full HTTP stack to parse the possible responses (e.g. what different encodings? Partial Content?). Use cURL.
The reason its currently failing is probably because the remote system is configured to use keepalives - which would again be solved by using a proper HTTP stack.
C.
I recommend, use curl for soap actions. http://www.zimbra.com/forums/developers/9890-solved-simple-soap-admin-example-php.html#post52586