Getting Zend_Http Final URL - zend-framework

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

Related

Can't update jira issue with REST API

I'm trying to do this with powershell, but I'm getting 400 errors:
$RESTURL = 'https://mycomp.atlassian.net/rest/api/latest/issue/PROJ-61'
$body = '{"fields":{"assignee":{"name":"me"}}}'
$restcreds = [System.Convert]::ToBase64String(
[System.Text.Encoding]::ASCII.GetBytes(('me' + ":" + 'mypass123'))
)
$httpheader = #{Authorization = "Basic $restcreds"}
$restParameters = #{
Uri = $RESTURL;
ContentType = "application/json";
Method = "PUT";
Headers = $httpheader;
Body = $body;
}
Invoke-RestMethod #restParameters
If I remove "body" from the request and change it to a get I get back data successfully. It seems I just get modify the ticket
If you get a 400 (bad request), then that means something is wrong in your request body.
The response body will contain a more detailed error message and will make it clear what you have to fix.
Without the error message, I can only make a guess:
I'm not sure if setting assignee to "me" works, unless "me" really is the name of a user. What happens if you try with a complete username or if you use "key" instead of "name"?
The fact that a GET request works fine shows that your credentials are correct, so it's not an authentication problem.

Guzzle returning a 404 on a valid URL

I'm using Guzzle with CurlAuthPlugin to authenticate. When I run the code, I get
Client error response\ [status code] 404\ [reason phrase] Not Found\ [url] https:\/\/api.buto.tv\/v2\/video\/tag\/v2\/video\/tag\/
The code I'm using is:
$client = new Client(<URL>);
// Add the auth plugin to the client object
$authPlugin = new CurlAuthPlugin(<APIKEY>, 'x');
$client->addSubscriber($authPlugin);
$response = $client->get('v2/video/tag/')->send();
But the URL is perfectly valid a I can paste that in to a browser and it works fine
I've also tried:
$client = new Client('https://api.buto.tv');
$request = $client->get('v2/video/tag/');
$request->setAuth('user', 'pass');
$response = $request->send();
But I get the same error. I have output the URL it's requesting with echo $request->getUrl(); and if I copy and paste the URL in to a browser, the URL is fine
I think you may be missing a slash '/' after api.buto.tv, so the url is resolving to 'https://api.buto.tvv2/video/tag/' instead of 'https://api.buto.tv/v2/video/tag/'.
$client = new Client('https://api.buto.tv/');
$request = $client->get('v2/video/tag/');

Get redirected url in perl

I want to get last of redirect URL.
like
url_1 : http://on.fb.me/4VGeu
url_2 : https://www.facebook.com/
I want to get url_2 by url_1 in perl.
Previous source is below.
sub get_redirect_location
{
my ($url) = #_;
my $ua = LWP::UserAgent->new;
$ua->proxy('http', 'SAMPLE_PROXY');
my $req = new HTTP::Request(GET => $url);
my $res = $ua->request($req);
return $res->headers_as_string;
}
Thanks in advance.
You can find the request that lead to a response using
$response->request()
You can get the previous response in the chain using
$response->previous()
All together:
while ($response) {
say $response->request()->uri();
$response = $response->previous();
}
You could look at WWW::Mechanize. I have used it before to do something like this.
http://search.cpan.org/~jesse/WWW-Mechanize-1.72/lib/WWW/Mechanize.pm#$mech->redirect_ok()
You may also find this post helpful:
Perl WWW::Mechanize (or LWP) get redirect url

facebook api /me/events/not_replied

I'm trying to get the events/not_replied from me, but all I get is an empty array....
I've tried those:
$facebook->api('/me/events/not_replied','GET');
and
$facebook->api('/me/events/not_replied');
and
$access_token = $facebook->getAccessToken();
$url = 'https://graph.facebook.com/me/events/not_replied?access_token='.$access_token;
$string = file_get_contents($url);
$json_a=json_decode($string,true);
and etc...
none works. Any idea?
You need to request the "user_events" permission.

Set Zend response into a variable

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);
}