I looked at the URL helper and URI class and I noticed that both of these work off the URL in the address bar. Is there a way I can use this helper or class with my own URL string? I want to retrieve the last segment of a URL I give and I don't want to resort to preg_match unless I need to. Is there a way to do this with codeigniter functionality?
If you have a string in the format of http://example.com/foo/bar (I presume that's what you mean by 'own URL string'?), you should be able to just do something like this:
$url = "http://example.com/foo/bar";
$parts = explode("/", $url);
$last = end($parts); // => bar
You can use:
$this->uri->segment($this->uri->total_segments())
or
array_pop($this->uri->segment_array())
if you want to use CI functionality.
Related
I am having issues testing a rest API. I want to trigger it from PHP by doing a file_get_contents.
This is my code so far
<?php
$url = 'http://domain:0000/rest/createUser?u=username&p=password&username=testuser&password=testpassword&email=user#domain.co.uk&';
$encodedUrl = urlencode($url);
$apicall = file_get_contents($url);
?>
This URL works from a browser, but as soon as I use file_get_contents it doesn't work.
Could the end server be blocking the use of file_get_contents? If so how? and how can I begin to test and troubleshoot this?
The issue is that you're using urlencode.
The urlencode function is used specifically for encoding strings inside a portion of a url. For example, you can use it to add data after the ?, and make sure that things like & turns into %26 and spaces turn into %20.
But it's not used to encode the entire url, it just makes the url invalid.
Try to remove the last "&" in your URL and then use the $encodedUrl instead of $url in the file get contents.
So try to turn this :
$apicall = file_get_contents($url);
INTO
$apicall = file_get_contents($encodedUrl);
Is there a way to dynamically add a slash to your rest url?
e.g. I want to be able to generate the following rest urls in one resource.
rest/blogpost/1
rest/blogpost/1/allInfo
given the resource below, i can achieve my first url. But is there a way to make the second url with /allInfo (optional in same lResource).
lResource = $resource("../rest/blogpost/:blogId", {
Or do I need a second resource like this?
lResource = $resource("../rest/blogpost/:blogId/allInfo", {
The problem with the second $resource is that allInfo isn't optional
If you make your second argument optional using the : you can make it to work.
var lResource = $resource("rest/blogpost/:blogId/:allInfo");
lResource.query({});
lResource.query({blogId:123});
lResource.query({blogId:123,allInfo:'allInfo'});
See my fiddle http://jsfiddle.net/cmyworld/NnHr4/1/ ( See Console log)
In my web application I have a url that looks like the following:
http://mydomain.com/search/index/list/for-sale/london/0/0/0/0
I would like to use the uri class and redirect to change $this->uri->segment(3); to map and then redirect.
So that once redirected and the segment has been changed the url would look like:
/search/index/map/for-sale/london/0/0/0/0
How would I go about doing this?
It may need some extra checks, but this would be a simple approach:
$segment_to_replace = "/".$this->uri->segment(3)."/";
$new_url = str_replace ($segment_to_replace, "/map/", current_url());
redirect ($new_url);
we are using Perl and cpan Modul FeedPP to parse RSS Feeds.
The Perl script runs trough the different items of the RSS Feeds and save the link to the database, liket his:
my $response = $ua->get($url);
if ($response->is_success) {
my $feed = XML::FeedPP->new( $response->content, -type => 'string' );
foreach my $item ( $feed->get_item() ) {
my $link = $item->link();
[...]
$url contains the URL to an RSS Feed, like http://my.domain/RSS/feeds.xml
in this case, $item->link() will contain links to the RSS article, like http://my.domain/topic/myarticle.html
The Problem is, some webservers (which provides the RSS feeds) does an HTTP refer in order to add an session ID to the URL, like this: http://my.domain/RSS/feeds.xml;jsessionid=4C989B1DB91D706C3E46B6E30427D5CD.
The strange think is, that feedPP seams to add this session-ID to the link of every item. So $item->link() contain links to the RSS article, like http://my.domain/topic/myarticle.html;jsessionid=4C989B1DB91D706C3E46B6E30427D5CD
Even if the original link does not contain an session ID.
Is there a way to turn of that behavior of feedPP??
Thank you for any kind of help.
I took a look through http://metacpan.org/pod/XML::FeedPP but didn't see any way to turn have the link() method trim those session IDs for you. (I'm using XML::FeedPP in one of my scripts and the site I happen to be parsing doesn't use session IDs.)
So I think the answer is no, not currently. You could try contacting the author or filing a bug.
IMHO, the behavior is correct: uri components which follow a semi-colon are defined part of the path (configuration parameter for interpretation), so when the uri is used to make a relative url into an absolute uri it needs to be copied as well.
You expect compatible behavior with '&' parameters, but they are not equal.
https://rt.cpan.org/Ticket/Display.html?id=73895
how can i append query strings to a url? i could of course do a (from controller action)
$currUrl = $this->getRequest()->getRequestUri();
$newUrl = $currUrl . '/something/else';
if the requestUri looks like /users thats fine. but what if the url looks like /users?page=1? then i will end up with something like /users?page=1/something/else which is wrong
That is not a reliable way to add parameters to the current request URI. Say for example that you're using the default module route, and your current URI is eg. /news. If you want to add params to the end, you should first append the action name, hence having: /news/index/something/else. You can see that it can become quite tedious to do this by hand. Zend Framework provides you methods to do this with ease. In your controller, you can do this to generate an URI based on the current one:
$router = Zend_Controller_Front::getInstance()->getRouter();
$url = $router->assemble(array('something' => 'somethingelse'));
If you want to keep the query string with the new URI, do after that:
if (!empty($_SERVER['QUERY_STRING']))
$url .= '?'.$_SERVER['QUERY_STRING'];