Tag : php-2

PHP – SOAP sample

PHP simple SOAP sample

SOAP server file (soapserver.php):


<?php
// Set a function in SOAP server file
function hello($someone)
{
return "Hello " . $someone . "!";
}
// Create a new SOAP server
$server = new SoapServer(null, array('uri' => "urn://g31.local/ex/res"));
// Add function to SOAP server
$server->addFunction("hello");
// Handle SOAP server
$server->handle();
?>

SOAP client file (soapclient.php):


<?php
// Set new soap client
$client = new SoapClient(null, array(
'location' => "http://localhost/soapsample/soapserver.php",
'uri' => "urn://localhost/soapsample/req",
'trace' => 1 ));
// Call SOAP
$return = $client->__soapCall("hello", array("User"));
// Dispay request/response informations
echo("\nReturning value of __soapCall() call: " . $return);
echo("\nDumping request headers:\n" . $client->__getLastRequestHeaders());
echo("\nDumping request:\n" . $client->__getLastRequest());
echo("\nDumping response headers:\n" . $client->__getLastResponseHeaders());
echo("\nDumping response:\n" . $client->__getLastResponse());
?>

Php – read all files and folders from a directory

Read all files and folders from a directory using scandir:


$dir = "/temp";
$files = scandir($dir);
print_r($files);

Read all files and folders from a directory using opendir:


$dir = "/temp/"
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "Filename: " . $file . ": filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}

PHP – runnig a script in command line

To run a script in command line:


php test.php

or


php -f test.php

To run a script in command line with parameters:


php test.php param1 param2 param3

or


php -f test.php param1 param2 param3

The list of used parameters can be found in:


$_SERVER['argv']

Php – script execution time

To view php script execution time insert next code into your script.

Insert next code at the top of the page:


<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
?>

Insert next code at the bottom of the page:


<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "Time execution: ".$totaltime." seconds";
?>