Archives for the 'PHP' Category
PHP whitespace fail
I had a problem with header(’Location: http://example.com’) not redirecting as it should. After fiddling a bit, I discovered that it worked before, but not after, this line:
$x = $this->foo($bar);
So what was in function foo()?
private function foo($val)
{
include_once ‘baz.php’;
$baz = new Baz();
return $baz->get_value($val);
}
I then proceeded […]
PHP foreach
The PHP foreach construct is very convenient, except that it creates a copy of the array. This is undesirable in the case of very large arrays. The following is a drop-in replacement for foreach, which operates directly on the array:
$arr = array(”one”, “two”, “three”);
reset($arr);
while (list(, $value) = each($arr))
{
echo “Value: $value<br />\n”;
}
Or
$arr […]
Calling a SOAP web service from PHP
The SOAP binding is RPC/encoded, and the WSDL is not published.
Using the PHP SoapClient:
define(’NEWLINE’, “<br />\n”);
// SOAP client
$options = array
(
‘location’ => ‘https://example.com:1234/sample/service’,
‘uri’ => ‘http://example.com/sample/namespace/data’,
’style’ => SOAP_RPC,
‘use’ => SOAP_ENCODED,
‘trace’ => true // in conjunction with $soapClient->__getLastRequest() below
);
$soapClient = new SoapClient(null, $options);
// SOAP header
$nsHeader = ‘http://example.com/sample/namespace/security’;
$elementName = ‘Security’;
$usernameToken->Username = new SoapVar(’user9′, XSD_STRING, null, null, null, $nsHeader);
$usernameToken->Password = […]
Calling a WCF service from PHP
The WCF service needs to use basicHttpBinding.
The PHP code:
define(’NEWLINE’, “<br />\n”);
// SOAP client
$wsdl = ‘http://example.com/SampleWcfService.SampleService.Service?wsdl’;
$soapClient = new SoapClient($wsdl, array(’cache_wsdl’ => 0));
// SOAP call
$sampleData->SampleProperty = “abc”;
$parameters->sampleId = 123;
$parameters->sampleData = $sampleData;
try
{
$result = $soapClient->GetData($parameters);
}
catch (SoapFault $fault)
{
echo “Fault code: {$fault->faultcode}” . NEWLINE;
echo “Fault string: {$fault->faultstring}” . NEWLINE;
if ($soapClient != null)
{
$soapClient = null;
}
exit();
}
$soapClient = null;
//echo “<pre>\n”;
//print_r($result);
//echo “</pre>\n”;
echo “Return value: {$result->GetDataResult}” […]
Using log4php
A how-to for the excellent log4php:
Rolling daily file
Log error statements into one file, and info statements into another
Send emails
Usage
include_once(’/var/www/example/components/log4php/2.0.0/Logger.php’);
class Example
{
private $logger;
function __construct()
{
$loggerConfig = ‘/var/www/example/config/log4php.xml’;
Logger::configure($loggerConfig);
$this->logger = Logger::getLogger(’example’);
}
public function doSomething()
{
$var = $this->getSomeValue();
$this->logger->debug(’in doSomething: $var = ‘ . $var);
try
{
$this->doSomethingSpecific();
}
catch (Exception $ex)
{
$this->logger->error($ex->getMessage());
throw new Exception(”There is a problem with Something. We apologize for any inconvenience caused.”);
}
$this->logger->info(”doSomething successful”);
}
}
log4php.xml
<log4php:configuration xmlns:log4php=”http://logging.apache.org/log4php/”>
[…]