Tuesday, November 6, 2012

Debugging in Zend Framework

Perhaps “debugging” is a bit too strong. However when you’re dumping an array in PHP, you’d probably prefer the print_r or var_dump.

echo '<pre>';
print_r($array);
echo '</pre>';

But did you know that in Zend Framework there’s a built in Zend_Debug?

Zend_Debug::dump($array);

Monday, November 5, 2012

POST with Zend_Http_Client

It’s a well know fact that you can preform HTTP requests with CURL. Zend Framework does the same job with Zend_Http. Especially Zend_Http_Client can be used to “replace” the usual client – the browser, and to perform some basic requests.

I’ve seen mostly GET requests, although Zend_Http_Client can perform various requests such as POST as well.

// new HTTP request to some HTTP address
$httpClient = new Zend_Http_Client('http://www.example.com/');
// GET the response
$response = $httpClient->request(Zend_Http_Client::GET);

Here’s a little snippet showing how to POST some data to a server.

// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);

Note that the request method returns a response. Thus if you are simulating a form submit action you can “redirect” to the desired page just like the form.

// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);
//echo $response->location;
ecgo $response->getBody();

If you want to send Authenticated POST Request with Zend_Http_Client, then you need to add the following parameter.

$client->setAuth('test_usernaem', 'test_password', Zend_Http_Client::AUTH_BASIC);

original post