I want to pass a url like http://example.com/test?a=1&b=2 in url segment of codeigniter.
I'm trying to pass something like this http://myurl.com/abc/http://example.com/test?a=1&b=2 and get the "http://example.com/test?a=1&b=2" url. What should be the best way to do this?
Set your URI protocol to REQUEST_URI in application/config/config.php , like this:
$config['uri_protocol'] = 'REQUEST_URI';
then use GET method:
$this->input->get('a');
EDIT:
Since http://example.com/test?a=1&b=2 is not encoded URL, it isn't possible. So first, I would encode URL with urlencode function like this:
urlencode('http://example.com/test?a=1&b=2');
it returns something like: http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2
So I would pass the URL like this:
http://myurl.com/?url=http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2
then get an example URL with GET method.
$this->input->get('url');
Use this technique to get the URL
$url = "http://example.com/test?a=1&b=2";
$segments = array($controller, $action, $url);
echo site_url($segments);
// or create a anchor link
echo anchor($segments, "click me");
Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:
application/core/MY_URI.php:
<?php
class MY_URI extends CI_URI {
function _filter_uri($str)
{
return rawurldecode(parent::_filter_uri($str));
}
}
// EOF
Related
I have a domain and id is coming in that domain like this
http://www.test.com/update_record_row/id/6356/
I want id value from the domain without passing ? instead of / in (query string)
Using $this->uri->segment() you get the URL parameter
$this->uri->segment(1)
For more ..https://www.codeigniter.com/userguide3/libraries/uri.html
ID
public function update_record_row($id)
{
echo "Id is :".$id;
}
or
Use URI segment
// if URL -http://www.test.com/update_record_row/id/6356/
$id = $this->uri->segment(3);
// if URL -http://www.test.com/update_record_row/6356/
$id = $this->uri->segment(2);
Use $this->uri->segment() to retreive passed value through url
If URL is http://www.test.com/update_record_row/id/6356/
You should use $this->uri->segment(3);
Codeigniter has awesome userguide.Just check this in in uri part.
https://www.codeigniter.com/userguide3/libraries/uri.html#uri-class
I want to remove some url in CI
if any echo like:
www.blabla.co/content/5DRwA/6Yt54/bla-bla
so, replace to:
www.blabla.co/content/bla-bla
nb: 6Yt54 and 5DRwA is a random value
How I remove URI Segment 2 from behind like that?
How can I solve it?
Try the uri class. First retrive uri with:
$array = $this->uri->segment_array();
Then remove second segment:
unset($array[2]);
Unfortunately your url doesnt seem to follow CI convention (you could use then $this->uri->assoc_to_uri($array)). So iterate over array to create uri.
$my_uri = ''; //better name it $my_uri not to conflict with $this->uri
foreach ($array as $value) {
$my_uri.= '/'.$value;
}
And then you can use your new uri f.e. to redirect:
redirect($uri);
ok, I have a url that calls a webservice, lets say that url is:
http://url.com/products/[productid]?Lanaugae=english$username=username&token=1234
What I'm doing is calling the webservice inside a function, grab the product ID from a get and pass it in and then fire off the function that does all the curl goodness.
The troublesome bit is the productid is in the middle of the url, which I declared globally, and the curl bit fires from another function. I think in .net you could say {0} in the place of [productid] then when you call the variable tell it what should go into [productid], can I do something similar in PHP?
So at the top of my class I want:
$this->url = 'http://url.com/products/{0}?Lanaugae=engish$username=username&token=1234';
this in my function I want to do something like:
$productid = $_GET["productid"];
$responseData = $this->curl_dat_thing($this->url, $productid);
...This possible/make sense?
Try using sprintf() like this:
$this->url = 'http://url.com/products/%s?Lanaugae=engish$username=username&token=1234';
$productid = $_GET["productid"];
$responseData = $this->curl_dat_thing(sprintf($this->url, $productid));
I'm not sure what's inside curl_dat_thing() but you should do this:
$this->url = 'http://url.com/products/'.$productid.'?Lanaugae=engish&username=username&token=1234';
Example:
...scms/contracts/set_pm/3/Monthly
this is the URL and i want to get the word monthly...I cant use uri->segment in my view so I'm asking if there's other way
Just retrieve the current_url() and explode it with /, but why do such cumbersome process when you have lots of options provided by CI.
If you cant use $this->uri in your view just put that segment in a variable and load the view like this:
$data['segment'] = $this->uri->segment(5); //your segment here you want in your view
$this->load->view('view', $data);
Now, you will get the segment in your view as $segment.
May be something like this
$actual_link = current_url();
$slashes = explode("/",$actual_link);
echo $element = $slashes[count($slashes)-1];
You can use explod function in PHP to get a tab with your string in input.
$tab = explode("/", $url);
$last = end($tab);
Considering contracts is your controller file name and set_pm is your function then you can also fetch it like this:
class Contracts extends CI_Controller {
function set_pm($id, $interval)
{
echo $id; //prints 3
echo $interval; // prints Monthly
}
}
I have a method
function checkin($var1){
$newVar1 = $var1;
....
...
}
I am calling it via Restful and I am passing it like this
$url = 'http://mydomain.com/controller/checkin/'.$var1;
Now i want to pass two variables but I am not sure how would it pick the second one
I guess I can do this
$url = 'http://mydomain.com/controller/checkin/'.$var1.'/'.$var2;
not sure what would I do on receiving end to make sure it knows what var to use where.
thanks
On the other end, you have to change your action method signature to
function checkin($var1, $var2){
// (...)
}
Another option is using Cake's named parameters. That would require a change in both the url and the action:
URL
$url = 'http://mydomain.com/controller/checkin/var1:'.$var1.'/var2:'.$var2;
Action method
function checkin(){
$var1 = $this->params['named']['var1'];
$var2 = $this->params['named']['var2'];
}