Pass variable into url parameter php [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to pass a variable from a form to another php file to create an xml file.
<?php
$sentxt = $_POST['text']; //this comes from the form php
$params = array(
'answer_url' => 'to_xml.php?msg="$sentxt"',
);
$response = $p->make_xml($params);
echo "$sentxt";
?>
Whenever I try to run this I run into a problem
The xml file keeps outputting "$sentxt" instead of the string passed to the $sentxt via the php form post.
The echo "$sentxt"; displays the right string has been passed through properly, but the string is never passed into the array.

You should replace ' with " for string interpolation.
<?php
$sentxt = $_POST['text']; //this comes from the form php
$params = array(
'answer_url' => "to_xml.php?msg=$sentxt",
);
$response = $p->make_xml($params);
echo $sentxt;
?>

Related

How do I get individual data from an API array in PHP? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to write a little webapp that will show some data. The data comes from an API. All data from the API I get displayed, but if I only want to display specific data, I always get an error.
I have not had much experience with API's yet, but all the tutorials I've done show the data via this echo method.
My current code:
<?php
$user_id = "3bb8d3bc-ab7a-45fb-8154-ed54897b2c4c";
$api_response = file_get_contents('https://r6tab.com/api/player.php?p_id='.$user_id);
$api_response_decoded = json_decode($api_response, true);
//foreach($api_response_decoded as $val) {
//echo $val, '<br>';
//}
echo $api_response_decoded->p_name;
?>
api_response Output:
"playerfound":true,"social":{"twitter":"","instagram":"","mixer":"","twitch":"","youtube":"","bio":"","esl":"","discord":"","background":"","aliases":"","embed":""},"seasonal":{"current_NA_mmr":0,"last_NA_mmr":0,"last_NA_mmrchange":0,"current_EU_mmr":3831,"last_EU_mmr":3881,"last_EU_mmrchange":-50,"current_AS_mmr":0,"last_AS_mmr":0,"last_AS_mmrchange":0,"total_casualwins":6,"total_casuallosses":5,"total_casualtotal":11,"total_casualkills":28,"total_casualdeaths":18,"total_rankedwins":38,"total_rankedlosses":24,"total_rankedtotal":62,"total_rankedkills":244,"total_rankeddeaths":201,"total_generalwins":44,"total_generallosses":29,"total_generaltotal":73,"total_generalkills":272,"total_generaldeaths":219,"total_totalbulletshits":3851,"total_totalhs":172,"total_totaltimeplayed":0,"bomb_wins":67,"bomb_losses":41,"bomb_total":108,"secure_wins":0,"secure_losses":0,"secure_total":0,"hostage_wins":1,"hostage_losses":0,"hostage_total":1,"favorite_mode":"bomb"},"matches":[{"casual_wlstatus":"won","casual_winslost":"1 Won","casual_datatime":"10\/02\/19","ranked_wlstatus":"won","ranked_winslost":"6 Won, 2 Lost","ranked_datatime":"10\/02\/19","next":"default","db_p_total_casualwins":1,"db_p_total_casuallosses":0,"db_p_total_casualkills":2,"db_p_total_casualdeaths":2,"db_p_total_rankedwins":6,"db_p_total_rankedlosses":2,"db_p_total_rankedkills":23,"db_p_total_rankeddeaths":23,"db_p_total_totalhs":10,"db_p_NA_currentmmr":0,"db_p_EU_currentmmr":3881,"db_p_AS_currentmmr":0,"NA_mmrchange":0,"EU_mmrchange":189,"AS_mmrchange":0},
My Error Message:
Trying to get property 'p_name' of non-object in C:\xampp\htdocs\R6S-Stats\stats.php on line 12
By using the true flag on json_decode, you're creating an associated array instead of an object. Remove that flag, and you'll be able to use the object methods:
$api_response_decoded = json_decode($api_response);
echo $api_response_decoded->p_name; //Scenus
If you want to keep it as an associated array, you can access the data like this:
echo $api_response_decoded['p_name'];
when use json_decode('dsfsdf', true);
$p_name = $api_response_decoded['p_name'];
when use json_decode('dsfsdf);
$p_name = $api_response_decoded->p_name;

PHP Return Array from function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a very simple web app that is capturing RFID tag reads and then submits it into the Database.
I have a function that was to pass the information through a filter and remove the duplicates and then return an array of unique tag reads.
The function looks like this
$txtarea = $_POST["rfid"];
rfid($txtarea);
function rfid($txtarea){
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
return $rfid_array1;
}
I then use Print_r to check the contents of the array to make sure it works.
When I run the code inside the function I do not get a result returned but when I run the following outside the function
$txtarea = $_POST["rfid"];
rfid($txtarea);
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
It returns the values correctly ?
I am very new to PHP so I apologize if this question seems a little basic.
The function rfid returns a value which you could capture in a variable.
$rfid_array1 = rfid($txtarea);
Note that you could shorten the function a bit:
function rfid($txtarea){
return array_unique(explode("\r\n", $txtarea));
}
Demo on https://3v4l.org/DY8Ts

Error when accessing array in PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to access the $_GET array in PHP like so:
<?php
$incl = $_GET("incl");
if ( $incl == "" ) { $incl = "home"; }
Whenever I access the script, I get a 500 error. Any idea why?
If you get an unexpected HTTP 500 error that you haven't sent yourself from PHP, this means there's an actual error in your script.
In this case, your problem is this line:
$incl = $_GET("incl");
$_GET is an array, so when you want to access values within it by key, you need to do so with [], not ():
$incl = $_GET['incl'];
Further, your check for $incl being empty should look like this:
if(empty($_GET['incl']){
$incl = 'home';
} else {
$incl = $_GET['incl'];
}

simplexml_load_file() with a variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm currently having an issue with simplexml_load_file(); my xml path is a url, that is rendered as a variable
$xurl = "domain/pathto/myfile.xml"; // This is actually a variable that returns the entire URL to where my xml file is -- this will change from file to file
$xmlpath = parse_url($xurl, PHP_URL_PATH); // to get the path of my xml file ex. /pathto/myfile.xml
$xmlpath = mb_substr($xmlpath, 1); // returns pathto/myfile.xml
here is where my problem is, when I put it into :
simplexml_load_file($xmlpath);
In my function, I get nothing appearing from the XML file
However, in my same function if I change it to
simplexml_load_file("pathto/myfile.xml");
My function works fine.
I did an echo on $xmlpath and it returns the pathto/myfile.xml just fine.
<?php echo $xmlpath; ?> // returns pathto/myfile.xml
What am I doing wrong?
EDIT: Phil
echo strcmp("pathto/myfile.xml", $xmlpath)
returns a 0.

php &variables in the URL aren't being defined in the page [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a messages page that loads communications between two users. The URL is message.php?u=[me]&p=[message parent id]&op=[other person], but $p and $op are not being defined in the page. When I echo each variable separately, $u appears everywhere from pre tag to the bottom of the document, but $p and $op do not echo anywhere. I tried deleting everything in .htaccess to see if that was causing a bug, but it wasn't. I can't think of what would cause this.
yourscript.php?var1=value1&var2=value2
You can use this by:
$var1 = $_GET['var1']; // $var1 = 'value1'
$var2 = $_GET['var2']; // $var2 = 'value2'
You can check whether or not these URL-parameters are set with isset()
(otherwise you create errors when you do not set them):
if (isset($_GET['var1'])) {
$var1 = $_GET['var1'];
} else {
die('usage: yourscript.php?var1=value1 !');
}

Categories