PHP client client
I have a problem with a php client.
What I can:
1) connect to tcp server using password
2) read line from tcp server
3) save xml string from tcp server to file once
4) close the connection
What I want:
1) save xml string from tcp server to file in a loop - delay 10 seconds (never close)
Technical information:
I have to read the first header response from the server, like "Hello stranger". Add my pass to the string and send it to the server. I did it freely.
TCP stream (XML file) from server end char NULL (0x00)
My code:
<?php
$cfgPort = "666"; //that not my port, server and pass :-D
$cfgTimeOut = "5";
$cfgServer ="myserver.pl";
$password = "donald";
$socket =fsockopen("$cfgServer",$cfgPort,$cfgTimeOut);
if (!$socket)
{
echo "Not conn\r\n";
}
else
{
echo "Conn!\r\n";
$key = fread($socket, 81);
//$key = fgets($socket, 82);
//what should I use fread or fgets ???
$key_password = $key.$password;
// log in
fwrite($socket, $key_password."\r\n");
//I checked response from server
echo fread($socket, 18);
//if authorized
//body response
$his = fread($socket, 2048);
$xml_clear = substr($his, 1, -1);
$file = 'xml.xml';
file_put_contents($file, $xml_clear);
fclose($socket);
}
echo "The end!\r\n";
?>
Next question:
Should I use CRON for this?
On telnet, I can send a header (password) packet once and I am waiting for a live response. Can I do this in PHP?
source to share
Well, you can use cURL to get the file and extract the string from it like this:
pre {
font-family: monospace, monospace;
font-size: 1em;
overflow: auto;
border: 1px solid #999;
page-break-inside: avoid;
display: block;
padding: 9.5px;
margin: 0 0 10px;
font-size: 13px;
line-height: 1.42857143;
color: #333;
word-break: break-all;
word-wrap: break-word;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 4px;
}
<pre>
<?php
$username='donald';
$password='donald';
$url='http://odedta.com/projects/tests/test.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$response=curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
</pre>
The result is a dump of the xml content. After all changes to line / files are complete, you can use file_put_contents()
and then run a CRON job to run that file every 10 minutes.
Here the CRON command runs this file every 10 minutes: php -f /home/yourusername/public_html/path/to/project/test.php
Version 1:
If you want to get the title:
curl_setopt($ch, CURLOPT_HEADER, true);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
Version 2:
If you want to send information to your server, you can use this example script:
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
Where $fields
is an array of variables or strings (in your case) that you want to send to the server.
As for adding your password to a string or whatever, just use PHP concat using a dot like: $password.$smystring
source to share