PHP – return multiple values in Array

PHP doesn’t allow return of multiple values – no way. I typically would create an array and set key value pairs and return the array. Until I found this much easier way of handling the situation:

function getXYZ()
    return array(4,5,6);

list($x,$y,$z) = getXYZ();

// Afterwards: $x == 4 && $y == 5 && $z == 6
// (This will hold for all samples unless otherwise noted)

Technically, you’re returning an array and using list to store the elements of that array in different values instead of storing the actual array. Using this technique will make it feel most like returning multiple values.

LDAP authentication using PHP

A simple script to test out LDAP authentication via PHP.


$host = 'ldap-host.domain.pvt';
$port = '636';
$protocol = 'ldaps';
$base_dn = 'ou=corp,dc=domain,dc=pvt';
$domain   = "@domain.pvt";

$username = 'username';
$password = 'Password';
$connection_string = "$protocol://$host:$port";
print "$connection_string\n";
$conn = ldap_connect($connection_string) or die("Could not connect: $connection_string");
print $conn."\n";
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$ldaprdn = $username.$domain;
$ldapbind = ldap_bind($conn, $ldaprdn, $password);
print $ldapbind."\n";
$search = ldap_search($conn, $base_dn, "(samaccountname=$username)");
$result = ldap_get_entries($conn, $search);
print $result['count']."\n";
print "End\n\n";


PHP Warning – date(): It is not safe to rely on the system’s timezone settings

Ever encountered this warning while executing PHP code?

PHP Warning:  date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting 
or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely 
misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. 
in/path/file.php on line 10

Solution – explicitly add a timezone to the php.ini file

date.timezone = "America/Los_Angeles"

A list of timezones is available here –

Serve and Consume API that requires a JSON and not POST Fields

In PHP, $_POST would give you the POST payload. I was trying to post a multi-dimentional JSON to an API but the $_POST wasn’t able to get data for a field that had a list of values within,

e.g. {“email_recipients”:[“”, “”]}

Therefore, I had to write the API so it accepts a JSON which can be read into the API and converted to an array as follows:

$post_json = file_get_contents("php://input");
$array_post_data = json_decode($post_json, true);

Now, the client CURL call would appear something like this:

$json = {"email_recipients":["", ""]};
$url = "";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
$response = curl_exec($curl);
$curl_error = curl_error($curl);


PHP – Making API calls on the internet via Proxy

I had to make API calls to the internet from one of my servers that didn’t have connectivity to the outside world. I had IT create an internal proxy for me and I used it in the CURL to make the API call. Here’s the generic function that I created that would take care of using the proxy if stated to do so.


//-- Function to POST data to URL
function postArray($url, $arr_data, $setInternetProxy = false) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL, $url);
	if($setInternetProxy) {
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_POSTFIELDS, $arr_data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FAILONERROR,true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	$response = curl_exec($ch);
	$error = curl_error($ch);
	$arr_response = array();
	$arr_response['response'] = trim($response);
	$arr_response['error'] = trim($error);
	return $arr_response;