Skip to main content

PHP Curl and You

I have 2 servers which I wanted to talk to each other, one is a web server running Ubuntu and the other a Windows Server 2008. My task was to be able to create Active Directory users using PHP and from what is available, I found a very neat library for PHP called adLDAP. It had all the necessary functions needed to manage your Active Directory save for the very troublesome fact that you need to use LDAP over SSL in order for the module to be able to create accounts and change passwords. Everything else works without LDAP over SSL.

At first I was trying my hand out in making it work without the LDAP over SSL. But after some research, I found out that it is required by Windows Server itself to accommodate account creation. Knowing a bit about certificates and on how modern browsers typically screws over displaying the page due to those 'certificates cannot be verified' errors, I had little choice in this matter.

At first I thought maybe that error prompt could be a minor nuisance but unfortunately, non technical people have certain difficulty in getting around that. I had to make a choice, usability or to create something not that elegant which works without any nuisance.

Being a code monkey, I decided to do the workaround.

At first I researched options on what other ways I could create an Active Directory account without using the Active Directory Users and Computers manager. Apparently, you could create users using the command prompt using the dsadd command. After some practice, I got the syntax I needed and the errors thrown if ever theres something wrong.

So I already have the command, how do I run it? Ive thought of just moving the user creation page to the Windows Server and opening it to the public, but as Ive told my friend, it would be like a sitting duck waiting to be shot down. I retained the approach to funnel all traffic to the web server and have the web server and the Active Directory server talk using the more secure local network.

But how the heck could I make an Ubuntu server and a Windows Server talk to each other? Well, Ive installed a web server in the Windows Server and made it run PHP. Now, the Ubuntu server, the Windows Server and myself could all talk and understand the same language: PHP.

It was something unelegant. Some problems are still there, like if my GUI is in the Ubuntu server, how can I invoke a POST to the Windows Server and get the output from that end. Upon researching POST and passing parameters, I discovered cURL. cURL is a client URL library, which in simple terms, is something that one could use to pass parameters to another URL and gets a return reply which could be used in the requesting machine. A simple diagram would be:

Ubuntu cURL -> Windows Server (regular form accepting POST values) -> Ubuntu cURL

Ive setup two pages, first the page in the Ubuntu web server which does the 'curling'. Second is the page that accepts and processes form submits, if you've coded dynamic web pages this would sound familiar. Its just a regular page where all your data goes to when you hit the submit button. Its normally in the action="thispage.php" parameter. Whatever the output of that page will be thrown back to the currently waiting page in the Ubuntu web server.

I had my fair share in minor troubles, one would be that my PHP installation is not compiled to support cURL.

I just invoked an install command using apt-get (logged in as root of course)
apt-get install php5-curl
after it got installed, I restarted my web server
service apache2 restart
After which, I fired up my code and see what result it throws me from the Windows end. After some minor tweaks, I got it correctly.

The important phases of using cURL in passing parameters to another page as I read through this helpful guide would be (4 steps):

1.  Prepare the data to be posted

$data = array();
$data['first_name'] = 'Jatinder';
$data['last_name'] = 'Thind';
$data['password'] = 'secret';
$data['email'] = '';
The upper part of this segment is plugging into an array the values you need to be passed. The lower half would be turning it into a single chunk of data which will be sent to the other end. In this example, the resulting $post_str would be “first_name=Jatinder&last_name=Thind&password=secret&” which is properly url encoded. 

$post_str = '';
foreach($data as $key=>$val) {
    $post_str .= $key.'='.urlencode($val).'&';
$post_str = substr($post_str, 0, -1);
2. Connect to the remote URL

This is the page where you will be sending in your data. In my case, instead of a public domain address, I used a local IP address. It would still be accessible since PHP is a server side language which executes the code on the server (which is also in the same local network)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '' );
3. Post (submit) the data 

curl_setopt($ch, CURLOPT_POST, TRUE);
The first half is telling cURL that a POST method will be invoked. cURL handles a multitude of methods including support for proxies, GET, POST, HTTPS and all that sugary stuff. The code below tells cURL to send the $post_str we made earlier to the URL in step 2.
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);

4. Fetch response and display it to the user

 The last part waits for the output from the other end and displays it, hence the echo $result. This will output whatever is displayed on the other end as if you are browsing through the page. In my case, I coded the output on the Windows end to be as simple as possible: throwing a text saying that the command issued was successful or there was an error. Upon getting the output from the Windows end, I display it to the Ubuntu side by processing the $result. Use your imagination, feel free to use string manipulation functions to make the output compliant with the look and feel of your page.
$result = curl_exec($ch);
echo $result;
For more information about cURL, visit the ever helpful PHP API 


Popular posts from this blog

Self Signed SSL Certificates

Ever wondered how to enable SSL or HTTPS on your site? If you dont want to pay for commercial SSL certificates, you could create self signed certificates for your site by following the instructions here: The instructions in the site above will make your default site HTTPS enabled. If you prefer having a commercial SSL, save your certificate files and key files in your server and edit the location on the /etc/apache2/sites-enabled/default to point to the directory where you stored those files.

Moving to a New Linux Web Based Torrent Client

For years, I have been using TorrentFlux (url here) as my primary torrent client situated in my Ubuntu download server. But as time went on, the developers completely abandoned the development of TorrentFlux which led to several forks which I think is still insufficient for my needs. Main GUI of TorrentFlux Ive checked several options which runs on a GUI-less environment. Since my Ubuntu server is just running on command line to save precious memory, I needed something bare, simple and is packed with features. Installing uTorrent Server is pretty straight forward. Download. Uncompress. Run. This is better than the approach of TorrentFlux which you need to setup LAMP server and create a database. More often than not, it happens to me that some of the data in the DB gets corrupted. I normally just reinstall the whole thing again. Main GUI of uTorrent Server To further elaborate on the setup process, I've gotten an excerpt from this thread which, quite simply discusses ho

iPhone 4 Carrier Unlock Finally Here

The wonderful people of iPhone-dev Team (or I think they're called just DevTeam now) has realeased their iPhone 4 carrier unlock. What this means is you can unlock the iPhone 4 s you've purchased from any locked countries like the U.S.(with baseband 1.59 ).  iPhone 3G and 3Gs also benefits from this release which unlocks phones with basebands 04.26.08 , 05.11.07 , 05.12.01 and 05.13.04 As an excerpt from their blog post : Version 1.0-1 of ultrasn0w works for: iPhone4 baseband 01.59 3G/3GS basebands 04.26.08, 05.11.07, 05.12.01 and 05.13.04 (If ultrasn0w doesn’t show when you search Cydia, add the repo: Quickest solution to unlock your phones is to visit which uses an exploit to jailbreak your device, and once you get the Cydia app, download (or add the repository if you havent) ultrasn0w. Restart and enjoy carrier freedom. Perhaps another implication of this with the iPhone 4 market now is that those people selling iPhone 4