I have this problem.
I used this sentence to get post info in CodeIgniter 1.7.2 and it works fine
function function1(){
$input_data = json_decode(trim(file_get_contents('php://input')), true);
$info = str_replace( '"', '', json_encode($input_data['info']));
}
My input json is this:
{
"info":"hello!"
}
But when I used these same lines in CodeIgniter 2.1.3 it doesn't work. I used echo $info but my answer is null. Anyone can help me? Where is the mistake?
This behavior is not CI related. It's most PHP concepts.
Your mistake is probably that you're trying to access something that doesn't exist.
What is the point to use file_get_contents('php://input')? Can't you just use $this->input->post(), or the uploadclass? (in CI context)?
Anyway, check what do you get in file_get_contents(), var_dumping it. I'm pretty sure that your NULL comes from $input_data assignment statment, so you're fetching the JSON in the wrong way at first.
I'm using a Controller as a WebService and It works fine in a previous work. So, I tested with this
function test(){
$m = $this->input->post('key');
echo $m;
}
and I'm sending a POST JSON data as follow:
{
'key':'hello!"'
}
and now I receive nothing. Some else?
Related
I am a newbie in php and rest and I'm trying to understand what is going on in my API when using echo vs return....
I've tried to get down to the simplest possible scenario to isolate an issue of my rest API not returning any value, so here is goes:
I have a test.php file on my server with following content:
<?php
if(function_exists($_GET['t1'])) {
echo $_GET['t1']();
}
else if(function_exists($_GET['t2'])) {
return $_GET['t2']();
}
function test() {
return json_encode("test...");
}
?>
I then make a simple request using, as unique header 'Content-Type: application/json`
https://www.eswys.ch/tmp/test.php?t1=test
https://www.eswys.ch/tmp/test.php?t2=test
And results are, respectively
"test..."
""
I really struggle to understand this, why is my returned value somehow "lost" - is there any explanation to this?!
Returning from a function does not actually render any content to the response. You may have seen people returning data in different frameworks but the framework is actually echoing the data for you behind the scenes.
return assigns a value to a function call (like a variable) and echo is simply output to the html page or possibly a terminal window.
For one of my Laravel Web app I want to log all the Request Parameters(Post as well as Get) in database in Json Format for that I am using $request->all() Method, which results in an exception when user tries to upload any file.
that's why I want a way to select only Serializable Parameters from the request.(for get as well as for post Requests) or a way to select all the request parameters except files.
Request::except([]) will not work for me since in Except method we will have to provide the file parameter names.
In my project, i used this except for many fields like below,
$input = $request->except('first_name', 'middle_name', 'last_name', 'address',...);
It is work fine for me.
I stored all the remain values into $input and store values from that input variable.
Please try this one.
In your case please take this debug code for test once, might be you like it to use in your current work
$allRequestParams = array_map(function($input) {
return !is_array($input) ? $input : false;
}, $request->all());
echo '<pre>';
print_r($allRequestParams);
echo '<pre/>';
die;
Since any of the answer didn't work for me I did lots of reading and some digging about laravel but still I could not find the specific solutions I was looking for, so I did a small hack, instead of using Laravel's Request Object and pulling parameters from there I simply used PHP's built in $_REQUEST parameter.
Eg.
$non_file_parameters = $_REQUEST;
$_REQUEST will have both Get as well as Post Parameters except file Parameters coz in Core PHP for files we have $_FILES super global variable.
Thanks guys for your efforts...
I need to make a PATCH request to a PHP application.
How can I get the data from that PATCH request inside that application?
If I had to do it with a POST, it would just be a simple access to the global $_POST variable.
I know that this has been solved, but for anyone who was hoping for an answer like
$_PATCH["name"];
there is a way to do that:
parse_str(file_get_contents('php://input'), $_PATCH);
then you can access it like $_GET["something"] and $_POST["something"] just do
$_PATCH["something"]
hope that helped someone :)
You can get data with php://input stream wrapper:
$data = file_get_contents('php://input');
Also make sure your web server supports PATCH requests, some are configured to respond only to GET and POST.
Since none of the above has worked for me in PHP 5.6, here's a solution that actually did.
I used this parse_raw_http_request($data) function by Christof.
And here's the code:
$_PATCH = [];
parse_str(file_get_contents('php://input'), $_PATCH);
parse_raw_http_request($_PATCH);
// From now on, the $_PATCH variable keeps all request arguments as well,
// and they're accessible under approprate keys like $_PATCH['yourKey']
i'm using : PHP 7.4
function patchMethod(){
parse_str(file_get_contents('php://input'), $_PATCH);
$body=[];
if (is_array($_PATCH)) {
foreach ($_PATCH as $key => $value) {
$body[$key] = $value;
}
}
return $body;
}
You have $_REQUEST superglobal containing all data we can get regardless the HTTP method used (GET, POST, PATCH, PUT)
I'm trying to learn reddit code and using this https://github.com/jcleblanc/reddit-php-sdk
There's a method in this library $reddit->getUser() which outputs null always. Everything else seems to be working though.
include_once("jcleblanc-reddit-php-sdk-4a4cc32/reddit.php");
$reddit = new reddit("my-username", "my-password");
$userData = $reddit->getUser();
var_dump($userData);
outputs null.
$response = $reddit->getListing("all", 5);
var_dump($response);
outputs what's expected just fine. Then what could be going wrong with $reddit->getUser()?
this is Jonathan LeBlanc, I created that library. I'm running tests right now and I don't seem to be seeing the same issues. This is the code I'm running with the library:
<?php
require_once("reddit.php");
$reddit = new reddit("USERNAME", "PASSWORD");
var_dump($reddit->getUser());
?>
When running that, I am seeing my user data object coming through. Are you still experiencing this issue?
Jon
i am having a code which parses the xml text which is obtained from google search
it used to working fine before , I think after updating my version infos it not working fine
what might be the problem i am getting the following error
Fatal error: Call to undefined method SimpleXMLElement::child_nodes() in /home/search.php in line 70
Please let me know how can i solve this problem
This code is used in smarty
And it gets the response string from curl.
SimpleXMLElement does not have a method called child_nodes. Were you looking for the children method?
okay many thanx for your answers .
i got solved my problem by using the domxml-php4-to-php5.php file i have just uploaded this file and included that file name in my file . And it got worked .
I got that file from this link : http://alexandre.alapetite.fr/doc-alex/domxml-php4-php5/
:)
use the following notation to iterate thru your xml (change 'myfile.xml' and 'tagName'):
<?php
include('simple_html_dom.php');
if (file_exists('myfile.xml')) {
$xml = simplexml_load_file('myfile.xml');
print_r($xml);
foreach( $xml->children() AS $child ) {
$name = $child->getName();
if ($name == 'tagName') {
foreach( $child->children() AS $grandchild ) {
// DO SOMETHING
}
}
}
}
?>
there are other more elegant ways to achieve this, but this is a simple beginner's way to do it. for technical info: http://us2.php.net/manual/en/class.simplexmlelement.php