json string passed in url which is encoded by urlencode - php

I am using CodeIgniter 3.x.
User have this data : %7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
User send me the request and pass the data as parameter in url. The last parameter in url is nothing but the users data.
http://localhost/codeigniter/myproject/myfunction/%7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
The encoded data nothing but a json string which he want to encode and then send me the request. If I parse it using php then it yields this : '{"uid":"1234","uname":"kishor"}'
But it is not accepted in url, it gives me Object not found! error in browser.
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
I tried changing $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';, but it doesn't work for me.

Assuming that your CodeIgniter is set up properly and routing things correctly then I recommend sending the JSON as a $_GET parameter:
http://localhost/codeigniter/myproject/myfunction/?json=%7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
So in CodeIgniter, you should be able to simply:
json_decode( $this->input->get( 'json' ) );

If you pass the below way than you have to pass argument in function.
http://localhost/codeigniter/myproject/myfunction/%7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
like this
function myfunction($param = ''){ echo $params; }

The way you are passing is not a proper way. If you need to get and decode that value, use following way in your controller function
i dont know which one is controller & function in your code, I guess myproject is a controller. So, i'm passing 3rd parameter Number 3 in segment
// it will return %7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
$json_value = $this->uri->segment(3);
$json_value = (array) json_decode(stripslashes(urldecode($json_value)));
var_export($json_value);
output
array (
'uid' => '1234',
'uname' => 'kishor',
)

Related

php variables and url and specifically ?'s

I'm trying to write a script which accepts a url such as rfile.php?url=https://example.com/file.php?alias=midlands how ever I want to be able to pass vairables over so it becomes rfile.php?url=https://example.com/file.php?alias=midlands?type=src how ever $type=$_GET['type']; comes up as null.
I having checked $url=$_GET['url']; returns the full url string of rfile.php?url=https://example.com/file.php?alias=midlands?type=src
Clearly I can fix this but seperating the features I planned to use via the type vairable but I thought I would ask if any one knows a way to pull in that url including the ?alias=midlands but stop at ?type=src ?
Example url - rfile.php?url=https://example.com/file.php?alias=midlands?type=src
<?php
/*
*/
header("Pragma: No-Cache");
// Initialise variables
$url=NULL;
$type=NULL;
// Check auth is set
if (!isset($_GET['url']))
{
echo "Could not service your request. url was not set";
}
// Check auth is set
if (!isset($_GET['type']))
{
echo "Could not service your request. type was not set";
}
$url=$_GET['url'];
$type=$_GET['type'];
echo $url;
echo $type;
?>
Thanks
Terran
I'm pretty sure that what you want is to pass parameters and their values into your URL and grab them with $_GET method.
If this is what you are looking for, then you are having a typo in your example URL. The ? tells the URL where to start looking for parameters. Each pair of parameter-value must be separated with character &. This means that the correct format of your URL in order to retrieve all those parameters is this one:
rfile.php?url=https%3A%2F%2Fexample.com%2Ffile.php%3Falias%3Dmidlands&type=src
Since you want to have a url as a parameter of the original url you should try to urlencode the parameter as above.
Probably your results will be:
$_GET['url'] => https%3A%2F%2Fexample.com%2Ffile.php%3Falias%3Dmidlands
and
$_GET['type'] => src
Then u will have to run urldecode again on your $_GET['url'] to grab the nested parameters. Hope I've helped.

Codeigniter spaces in url

I have a method which returns all jobs by category.
The problem it's when i have spaces in the category name. How could I access those results?
for ex if i access http://localhost/management_system/Job/get_jobs_by_cat/Architecture it will return all the jobs from the Architecture category.
But when i try to access the category Information Technology I can't do it while I have spaces into the url, so I've tried with _ - and i didn't get any response.
How I can fix this issue?
You would likely want to use urldecode() to decode the url parameters in the controller function processing this logic. In your controller, do something like this:
$jobCat = urldecode($this->uri->segment(4));
Then, you would pass $jobCat to your model.
Here are some other Stack Overflow links that might help your cause.
How to pass parameters with space to controller from URL in
codeigniter?
Php - Codeigniter url spaces
what is the use of $this->uri->segment(3) in codeigniter
pagination
You need to URL encode Information Technology.
$category = 'Information Technology';
$encodedCategory = rawurlencode($category);
$url = 'http://localhost/management_system/Job/get_jobs_by_cat/' . $encodedCategory;
echo $url;
// http://localhost/management_system/Job/get_jobs_by_cat/Information%20Technology

JSON decode on Bit2Check.com API response

I'm using the Bit2Check.com API. I want to check emails registered on PayPal. But after supplying email address to the API, I get this response:
{"Paypal":"Linked"}
I just want to grab the "Linked" part.
You are providing me with no code, so I will have to try to answer your question in general.
You probably have
{"Paypal" : "Linked"}
stored within some variable. To access the "Linked" part, you need to json_decode() it. Code retrieving just the "Linked" string would like something like this.
$yourVariable = '{"Paypal" : "Linked"}';
$decoded = json_decode($yourVariable);
// Access only linked part
$onlyLinked = $decoded->Paypal;
// Or maybe you are interested in boolean value?
$isLinked = ($decoded->Paypal == "Linked") ? true : false;
To read more about json_decode(), visit this article on php.net.

Curl and codeigniter passing in POST data from an from into the url

Tying myself in knots a little bit with cURL. Using codeigniter for my framework and downloaded the CURL lib from getsparks.org/packages/curl .
I have this URL http://localhost:8984/rest?run=loc.xq&$a=York so this is a Rest for BaseX that I pass in a city (like York) and it shows me a carpark, This works in my browser. So what i wanted to do is have a user type the name into a form and pass that to basex and display the results. I have me view, controller set up, and this is currently the code I have in the controller
$this->load->library('curl');
$url = "http://localhost:8984/rest?run=loc.xq&$a=";
$datai = array($this->input->POST('carpark_location'));
$results = $this->curl->simple_get($url,$datai);
so this to me looks right, but its not, comes back with an error,
Message: Undefined variable: a
which I am guessing its picking $a out of the text as a variable, how would I make it ignore that?
not sure if just that will solve my issue but printing out the $url and $datai looks like below?
http://localhost:8984/rest?run=loc.xq&=Array ( [0] => York )
what am I missing?
Try this instead:
$url = 'http://localhost:8984/rest?run=loc.xq&$a='
Because it is inside single quotes instead of double quotes, PHP should treat $a as if it is part of the string instead of as a variable.

Passing info via URL and parsing it with PHP

I need to pass information via a URL to a PHP function that parses the received data.
So basically when they click on the specific link, it will go to the specific page, but also send through the arguments and values for the waiting PHP function to parse them and depending on what the values are do specific functions.
eg: http://www.mydomain.com/arg=val&arg=val&arg=val (not sure about how I built that URL either)
Your can access the parameters via the array $_GET.
Note: in your example, you basically send just one paramter. PHP cannot handle multiple parameters with the same name.
You can build URLs that contain data from variables using string concatenation or string parsing.
If you browse the web, you will recognize, that the query string (the parameters) always follow a ? in the URL. Read about URLs and the URI syntax.
Probably also interesting for you to read: Variables From External Sources which describes how to deal with data sent via POST or GET or cookies.
Generate URL:
<?php
$data = array(
'first_name' => 'John',
'last_name' => 'Doe',
'motto' => 'Out & About',
'age' => 33
);
$url = 'http://example.com/?' . http_build_query($data);
?>
Pick data:
<?php
$data = array();
if( isset($_GET['first_name']) ){
$data['first_name'] = $_GET['first_name'];
}
// Etc.
?>
You can access url parameters that follow the ? in at the end of the URL using the $_GET superglobal, e.g.
// url: http://mydomain.com/mypage.php?arg=Hello
echo $_GET['arg'];
Parameters passed in the query string are available in the $_GET superglobal array.
You can build the URL like the following (note the ? )
http://www.domain.com/page.php?arg1=val&arg2=val
Then in your PHP using the following code by using REQUEST you can get the data from either GET or POST parameters.
$arg1 = $_REQUEST['arg1'];

Categories