PHP Notes

Table of Contents

Things to look at

Books to read

Magazines/Publications & Groups

Saving a file

writeafile.php

SOAP

SOAP calls and debug

<?php
$client = new SoapClient($WebSvcWSDL,array(	'login'     => $login,
						'password'  => $password,
						'location'  => $WebSvcURL,
						'trace'     => 1,
						'exceptions' => 0));
//trace allows getLastRequest and getLastResponse to be called 
//- these show the SOAP XML send and recieved
//exceptions stops the SoapClient calls from throwing exceptions

$result = $client->commandfromWebSvcWSDL(array('thecommand' => $command));

print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>\n";
?>

SOAP Exceptions

<?php
try
{
    $client = new SoapClient($WebSvcWSDL,array(	'login'     => $login,
						'password'  => $password,
						'location'  => $WebSvcURL));
    $result = $client->commandfromWebSvcWSDL(array('thecommand' => $command));
}
catch (SoapFault $e)
{
    echo("\n<br />Error making SOAP call<br />\n");
    echo $e->getMessage();
    echo("\n");
    echo $e->faultcode;
    echo("\n");
    echo $e->faultstring;
}

?>

Examples

CUCM AXL (SOAP) Example
taken from Cisco CUCM AXL (SOAP) interface example in the Cisco Developer Network Administration XML page

<html>
<head>
  <title>PHP Test</title>
</head>
<body>
<?php
 $client = new SoapClient("/var/www/AXLAPI.wsdl",
 array('trace'=>true,
       'exceptions'=>true,
       'location'=>"https://10.1.2.3:8443/axl",
       'login'=>$adminid,
       'password'=>$password,
       ));
 $response = $client->getUser(array("userid"=>"jsmith"));
 echo("First Name: ".$response->return->user->firstname)."<br>";
 echo("Last Name: ".$response->return->user->lastname);
?>
</body>
</html>

SOAP References

Quick tutorial on PHP SOAP extension
Dr. Herong Yang's PHP SOAP Extension Tutorial
SOAP commands
Cisco CUCM AXL (SOAP) interface example

SimpleXMLElement::xpath

http://www.php.net/manual/en/function.simplexml-element-xpath.php

PHP Expect - expect_expectl

http://ca2.php.net/manual/en/function.expect-expectl.php

Splitting multiline text box into array

Taken from http://www.phpfreaks.com/forums/index.php?topic=157738.
<?php
$text = $_POST['userlist'];
$array = explode('\n', $text);
?>

Flushing HTML output

flush();

PECL extensions

http://ca3.php.net/manual/en/install.pecl.pear.php

Using an extension

in your php.ini, put a line
  extension = dir/whatever.so
Check to make sure it loaded using the phpinfo() call.

Alternatively, load in php script using
  dl("dir/whatever.so");

Building AJAX Applications with PHP and HTML_AJAX

http://devzone.zend.com/article/4201-Building-AJAX-Applications-with-PHP-and-HTML_AJAX

PHP and named pipes

http://my.opera.com/zomg/blog/2007/08/29/php-and-named-pipes

Lifted Scripts

Errors

taken from Matthew Weier O'Phinney's Weblog
    *  To display errors:
          o In you php.ini file, set "display_errors = On", or
          o In your script, add the line "ini_set('display_errors', true);"
    * To show notices, warnings, errors, deprecation notices:
          o In you php.ini file, set "error_reporting = E_ALL | E_STRICT", or
          o In your script, add the line "error_reporting(E_ALL | E_STRICT);"

Alternatively, you can create a file with the lines:

<?php
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', true);

and then set the php.ini setting 'auto_prepend_file' to the path to that file.

NOTE: do not do any of the above on a production system! PHP's error messages often reveal a lot about your applications, including file layout and potential vectors of attack. Turn display_errors off on production machines, set your error_reporting somewhat lower, and log_errors to a file so you can keep track of what's going on on your production system.

The second part of the question was how to run a PHP script on the command line. This is incredibly simple: php myscript.php. No different than any other scripting language.

You can get some good information by using some of the switches, though. '-l' turns the PHP interpreter into a linter, and can let you know if your code is well-formed (which doesn't necessarily preclude runtime or parse errors). '-f' will run the script through the parser, which can give you even more information. I typically bind these actions to keys in vim so I can check my work as I go.

If you plan on running your code solely on the commandline, add a shebang to the first line of your script: #!/path/to/php. Then make the script executable, and you're good to go. This is handy for cronjobs, or batch processing scripts.

All of this information is readily available in the PHP manual, and the commandline options are always available by passing the --help switch to the PHP executable. So, start testing your scripts already!

Running PHP from the command line

http://www.creativebloq.com/web-design/how-use-php-command-line-9112845

php -f <php script>
php -f hello-world.php