Set Zend response into a variable - zend-framework

I have an action that returns a JSON. I need to call it from another controller and I need to get this response into a variable to parse the JSON.
I've tried:
private function makeListFromUrl($menu)
{
$req = new Zend_Controller_Request_Http();
$req->setRequestUri('/module/controller/get.json/');
$res = new Zend_Controller_Response_Http();
$dis = $this->getFrontController()->dispatch($req, $res);
$dis->dispatch($req, $res);
$json = $res->getBody();
return Zend_Json::decode($json);
}
But this code causes the front controller to render the action, overriding the actual action. I just want to make a request, get the response into a variable, while leaving the actual request untouched.
Thanks.

i have a simple solution to this, not sure if it's the best, but worked very well.
$actionHelper = new Zend_View_Helper_Action();
$var = $actionHelper->action('action', 'controller', 'module', $params);
same way you'd do inside the view, but in the controller.
i hope this can help somebody.

You have to set returnResponse(true) for the FrontController to send the response back.
private function makeListFromUrl($menu)
{
$req = new Zend_Controller_Request_Http();
$req->setRequestUri('/module/controller/get.json/');
$front = Zend_Controller_Front::getInstance();
$front->returnResponse(true);
$response = $front->dispatch($requestObj);
$json = $res->getBody();
return Zend_Json::decode($json);
}

Related

Replace Contents of Request Object in Slim Middleware

I'm encrypting a header value and params of the request object. In the middleware.. I decrypt the values and would I like replace the values.
Tried the below. Doesnt seem to be working.
new \Slim\Http\Request($method, $uri, $headers, $cookies, $serverParams, $body)
For - $response = $next($request, $response); I tried the below
$response = $next(new \Slim\Http\Request($request->getMethod(),$request->getUri(), $arr, $request->getCookieParams(), $request->getServerParams(), $request->getBody()), $response);
Any help would be very much appreciated.
Try this to replace the header value:
$value = $response->getHeaderLine('MyHeader');
// ...
$response = $response->withHeader('MyHeader', $value);

Parse HTTP:Response object

I am having some difficulties getting results from a form via Perl. I believe I have successfully found the form and submitted the value I want to the appropriate field, but am unsure of how to turn the response object into something useful (If I print it out it shows up as the following).
HTTP::Request=HASH(0x895b8ac)
Here is the relevant code (assume $url is correct)
my $ua = LWP::UserAgent->new;
my $responce = $ua->get($url);
my #form = HTML::Form->parse($responce);
my $chosen = $form[0];
$chosen->value('netid', $user);
my $ro = $chosen->click('Search');
What can I do to make $ro useful?
Thanks!
To quote the HTML::Form docs on click:
The result of clicking is an HTTP::Request object that can then be passed to LWP::UserAgent if you want to obtain the server response.
So you can do:
my $ua = LWP::UserAgent->new;
my $response = $ua->get($url);
my #form = HTML::Form->parse($response);
my $chosen = $form[0];
$chosen->value('netid', $user);
my $ro = $chosen->click('Search');
# If you want to see what you're sending to the server:
print $ro->as_string;
# Fetch the server's response:
$response = $ua->request($ro);
What you do with $response next depends on what you're trying to do.
P.S. "responce" is usually spelled without a C. But HTTP does have a history of misspellings. (I'm looking at you, "Referer".)

PEAR Mail, Mail_Mime and headers() overwrite

I'm currently working on a reminder PHP Script which will be called via Cronjob once a day in order to inform customers about smth.
Therefore I'm using the PEAR Mail function, combined with Mail_Mime. Firstly the script searches for users in a mysql database. If $num_rows > 0, it's creating a new Mail object and a new Mail_mime object (the code encluded in this posts starts at this point). The problem now appears in the while-loop.
To be exact: The problem is
$mime->headers($headers, true);
As the doc. states, the second argument should overwrite the old headers. However all outgoing mails are sent with the header ($header['To']) from the first user.
I'm really going crazy about this thing... any suggestions?
(Note: However it's sending the correct headers when calling $mime = new Mail_mime() for each user - but it should work with calling it only once and then overwriting the old headers)
Code:
// sql query and if num_rows > 0 ....
require_once('/usr/local/lib/php/Mail.php');
require_once('/usr/local/lib/php/Mail/mime.php');
ob_start();
require_once($inclPath.'/email/head.php');
$head = ob_get_clean();
ob_start();
require_once($inclPath.'/email/foot.php');
$foot = ob_get_clean();
$XY['mail']['params']['driver'] = 'smtp';
$XY['mail']['params']['host'] = 'smtp.XY.at';
$XY['mail']['params']['port'] = 25;
$mail =& Mail::factory('smtp', $XY['mail']['params']);
$headers = array();
$headers['From'] = 'XY <service#XY.at>';
$headers['Subject'] = '=?UTF-8?B?'.base64_encode('Subject').'?=';
$headers['Reply-To'] = 'XY <service#XY.at>';
ob_start();
require_once($inclPath.'/email/templates/files.mail.require-review.php');
$template = ob_get_clean();
$crfl = "\n";
$mime = new Mail_mime($crfl);
while($row = $r->fetch_assoc()){
$html = $head . $template . $foot;
$mime->setHTMLBody($html);
#$to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <'.$row['email'].'>'; // for testing purpose i'm sending all mails to webmaster#XY.at
$to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <webmaster#XY.at>';
$headers['To'] = $to; // Sets to in headers to a new
$body = $mime->get(array('head_charset' => 'UTF-8', 'text_charset' => 'UTF-8', 'html_charset' => 'UTF-8'));
$hdrs = $mime->headers($headers, true); // although the second parameters says true, the second, thrid, ... mail still includes the To-header form the first user
$sent = $mail->send($to, $hdrs, $body);
if (PEAR::isError($sent)) {
errlog('error while sending to user_id: '.$row['id']); // personal error function
} else {
// Write log file
}
}
There is no reason to keep the old object and not creating a new one.
Use OOP properly and create new objects - you do not know how they work internally.

Getting Zend_Http Final URL

Making a simple request like:
$client = new Zend_Http_Client('http://example.org');
$response = $client->request();
How can I get the final URL after the redirects?
I have not seen a way in the documentation or the API docs unless I'm missing something.
Thanks in advance.
Zend_Http_Client update the last URL into Zend_Http_Client->uri property if there is redirect.
$sourceUrl = 'http://google.com';
$client = new Zend_Http_Client($sourceUrl);
$response = $client->request();
$finalUrl = $client->getUri()->__toString();
var_dump($sourceUrl);
// string(17) "http://google.com"
var_dump($finalUrl);
// string(25) "http://www.google.com:80/"
Not tested :
$response->getHeader('Location');
Get the last request from the client and then extract the headers.
$client = new Zend_Http_Client('http://webonyx.com');
$response = $client->request();
$lastHeaders = Zend_Http_Response::extractHeaders($client->getLastRequest());
// $lastHeaders['host'] will be your final redirected host

Adding authHeader to Perl SOAP::Lite request

I am having some trouble creating a request to this WSDL that works; it requires authHeaders and I am not having much luck adding them. This is what I am trying:
# make proxy for the service
my $soap = SOAP::Lite->service($wsdl);
# add fault hanlder
$soap->on_fault(
sub { # SOAP fault handler
my $soap = shift;
my $res = shift;
# Map faults to exceptions
if(ref($res) eq '') {
die($res);
}
else {
die($res->faultstring);
}
return new SOAP::SOM;
}
);
# authentication request headers
my #headers = (
SOAP::Header->name('user')->value('myemail#whatever.com')->uri($apins),
SOAP::Header->name('password')->value('mypassword')->uri($apins),
SOAP::Header->name('appName')->value('TestApp')->uri($apins),
SOAP::Header->name('appVersion')->value('0.02')->uri($apins)
);
# request method
print $soap->getCompanyInfo('NB', #headers);
The response I get when doing this is:
String value expected instead of SOAP::Header reference
The method I am requesting has two string parameters, both optional. And suggestions?
I was able to get help form the SOAP::Lite mailing list. If I want to pass my own headers, I have to use the call method instead of the actually method name.
# create header for requests
my $authHeader = SOAP::Header->name("xsd:authHeader" =>
\SOAP::Header->value(
SOAP::Header->name('xsd:user')->value($s7user)->type(''),
SOAP::Header->name('xsd:password')->value($s7pass)->type(''),
SOAP::Header->name('xsd:appName')->value('TestApp')->type(''),
SOAP::Header->name('xsd:appVersion')->value('0.03')->type('')
));
# create data to pass as method paramaters
my $params = SOAP::Data->name('ns:email')->value($s7user)->type('');
# request method
$soap->call('checkLogin', $params, $authHeader);
In order to use the call method, you will need to define a proxy (endpoint) on your soap object. Hope this is helpful for someone else down the road.