For this example, let’s say you have a site you want to post data to so a newly trained employee is added to a mailing list. In the older days of php, you could do this with http_post_params; however, this function is now deprecated. Instead, you can use the curl method, or you can use some Zend libraries.
Since this is a post on using Zend, I think you know which one I chose to do.
The following example is a really simple one. I’m just using a configuration file to hold my options and parameters to use for creating a curl request. Then I have a script that reads the config file, creates the request, sends the request, and then checks the response.
The Configuration File
; --configuration file /configFile/myConfig.ini [production] ;connection info curl.mailingList.adapter = Zend_Http_Client_Adapter_Curl curl.mailingList.uri = "http://www.example.com/mailinglist/api/add/" ;authentication/post parameters curl.mailingList.postParameter.APIUSER = "admin_user" curl.mailingList.postParameter.APIPWD = 'some_password' ;configuration options curl.mailingList.options.curloptions[CURLOPT_RETURNTRANSFER] = true curl.mailingList.options.curloptions[CURLOPT_HEADER] = false curl.mailingList.options.curloptions[CURLOPT_ENCODING] = "" curl.mailingList.options.curloptions[CURLOPT_AUTOREFERER] = true curl.mailingList.options.curloptions[CURLOPT_CONNECTTIMEOUT] = 120 curl.mailingList.options.curloptions[CURLOPT_TIMEOUT] = 120 curl.mailingList.options.curloptions[CURLOPT_MAXREDIRS] = 10 curl.mailingList.options.curloptions[CURLOPT_POST] = 1 curl.mailingList.options.curloptions[CURLOPT_VERBOSE] = 1 curl.mailingList.options.timeout = 120 curl.mailingList.options.maxredirects = 10
Performing the Curl
To actually perform the curl, we will need to load the configuration file into our code and then prepare the curl to send.
<?php /** * Create a post request to add a new trainee to the mailing list * * PHP Version 5 * * @created 10/12/2011 * @updated 10/12/2011 */ /** * Include the Zend_Loader class */ require_once 'Zend/Loader.php'; //load the needed Zend libraries Zend_Loader::loadClass('Zend_Config_Ini'); //for reading the configuration file Zend_Loader::loadClass('Zend_Http_Client_Adapter_Curl'); Zend_Loader::loadClass('Zend_Http_Client'); //read in the configuration file $config = new Zend_Config_Ini('/configFile/myConfig.ini', 'production'); //setup our post params (subscribees is the name of the form field) $postParam = array('subscribees' => 'joeExample@corporation.com'); //try to create and send the curl try { //setup the curl connection $adapter = new Zend_Http_Client_Adapter_Curl(); $adapter->setConfig($config->curl->mailingList->options->toArray()); //instantiate the http client and add set the adapter $client = new Zend_Http_Client($config->curl->mailingList->uri); $client->setAdapter($adapter); //add the post parameters from our config file (the authentication part) $client->setParameterPost( $config->curl->mailingList->postParameter->toArray() ); //add our post parameters to the request $client->setParameterPost($postParam); //perform the post, and get the response $response = $client->request(Zend_Http_Client::POST); //check the response for success if (strpos($response, "<h5>Successfully subscribed:</h5>") !== false) { echo "<p>Added {$fullName} to trainee email list successfully.</p>"; } else if (strpos($response, "Already a member") !== false) { echo "<p>Already a member of the trainee email list"; } else { echo "<p style='color: red;'>ERROR: Not added to the trainee email list</p>"; } } catch (Zend_Http_Client_Adapter_Curl_Exception $e) { echo "<p style='color: red;'> ERROR: Failed to add the employee to the trainee email list.</p>"; } catch (Zend_Http_Client_Exception $e) { echo "<p style='color: red;'> ERROR: Failed to add the employee to the trainee email list.</p>"; } ?>
As you can see, the script is not very complex. But it could be expanded to handle logging of errors and other functionality.