POST parameters passed via url empty php - php

This seems like a very trivial question, but I'm new to php, so please bear with me.
This is the test php script I wrote:
<?php
$str = $_POST['str'];
echo('string entered: '.$str);
I post a request by typing this into the url:
myurl.com/test.php?str=Hello
Now, the response I'm getting is:
string entered:
What am I doing wrong here?
Thanks!

Parameters in URL are GET, so you need to access them like
$param = $_GET['str'];

You're submitting a GET request. To access the parameter, use $_GET['str']

You're passing the variable as a GET parameter, rather than POST. If you want to access the variable this way, you will need to try this:
<?php
$str = $_GET['str'];
echo ('string entered: '.$str);

Related

Unable to collect values from array URL in PHP

3rd party passes the details to the URL. It makes get request to the URL with details as url-encoded HTTP query parameters.
following is the URL sample :
http://_______.com/customer_missed_call.php?CallSid=e9536ef16460727558c0db17349021b0&From=09513325525&To=08039511763&Direction=incoming&DialCallDuration=0&StartTime=2016-02-10+14%3A56%3A17&EndTime=0000-00-00+00%3A00%3A00&CallType=callattempt&DialWhomNumber=&Created=Wed%2C+10+Feb+2016+14%3A56%3A17&flow_id=67475&tenant_id=24683&CallFrom=09513325525&CallTo=8039511763&ForwardedFrom=&CurrentTime=2016-02-10+14%3A56%3A17
I want to collect the values of 'CallSid' and 'From' from this url.
I tried following codes
<?php echo $_GET; ?>
and
<?php echo utf8_decode(urldecode($_GET)); ?>
and
$URL = $_SERVER['REQUEST_URI'];
$parsedURL=parse_url($URL);
$URLQ=$parsedURL['CallSid'];
echo $parsedURL;
but did not got values for any one of the above code. Please try to give effective solution for the above problem. Thanks in advance.
They are missing an ? after the .php
It should be like this:
http://_______.com/customer_missed_call.php?CallSid=e9536ef16460727558c0db17349021b0&From=09513325525&To=08039511763&Direction=incoming&DialCallDuration=0&StartTime=2016-02-10+14%3A56%3A17&EndTime=0000-00-00+00%3A00%3A00&CallType=callattempt&DialWhomNumber=&Created=Wed%2C+10+Feb+2016+14%3A56%3A17&flow_id=67475&tenant_id=24683&CallFrom=09513325525&CallTo=8039511763&ForwardedFrom=&CurrentTime=2016-02-10+14%3A56%3A17
The "?"-delimiter is missing in the URL
Edit: Please provide how you call the URL (the code for the request).
$_GET['CallSid']; should do the trick normally
One of the things that is likely causing an issue is that parse_url returns an array with predefined keys. The key for the query is 'query' ($parsedURL['query'];)
However using the $_GET method is much more easy to read and is easier than getting the query string and manually parsing it. To access values in it, you also need to specify a key, not just call the array itself. e.g.:
$callSID = $_GET['CallSid'];
$from = $_GET['From'];
Hope that helps

PHP Variables not passing to url correctly from API

I'm trying to use the Premium URL Shortener script from codecanyon, I have asked for support but they seem to be a little busy, so the response time is not to quick.
The issue I have is when the API sends the request to the url shortener script with the following shortened query for example purposes:
$short = "http://myurl/api?api=MYAPI&format=text&url=http://myfullwebsite.com/email/quote.php?fullname=$fullname&address=$address&emailaddress=$emailaddress";
Although the variables are being placed in the script correctly using echo function at the end of the script after the api request is sent shows they are correctly inserted like so:
http://myurl/api?api=MYAPI&format=text&url=http://myfullwebsite.com/email/quote.php?fullname=Dan Smith&address=12 Main Street, London&emailaddress=dan#smith.com
However if I click the shortened url provided to me from the script I only get the following url string appear in the browser:
http://myfullwebsite.com/email/quote.php?fullname=Dan
It seems as soon as there is a space or even if there is no second name such as Dan Smith and only Dan is the available name, it will not even apply the second ampersand or & sign.
I have tried to use urlecode() but still no joy and I've been pulling my hair out for the last 3 days!
As a novice beginner it has been somewhat difficult to try and achieve the end result and it seems unreachable so I would appreciate any kind help or advice if possible, Maybe I'm missing something so simple?
I've thought of having the url query build from an array of variables but as a novice I've tried one way and failed so not sure if I have done it wrong.
Here is my full api code where I have tried both with SESSION and GET but that is not the problem as the end result echos to the browser with the variables there.. it's only when you follow the shortened url link that you see they're missing.
<?php
session_start();
$fullname = htmlspecialchars($_GET["fullname"]);
$address = htmlspecialchars($_GET["address"]);
$postcode = htmlspecialchars($_GET["postcode"]);
$emailaddress = htmlspecialchars($_GET["emailaddress"]);
$short = "http://myurl/api?api=MYAPI&format=text&url=http://ukhomesurveys.co.uk/email/quote.php?fullname=$fullname&address=$address&emailaddress=$emailaddress";
echo $short;
// Using Plain Text Response
$api_url = $short;
$res= #file_get_contents($api_url);
if($res){
echo $res;
}
?>
Hope I covered everything and hope I have not confused anyone. Thanks.
I think the good choice here is to encode your query with base64 and then pass it to the shortener. In your http://myfullwebsite.com/email/quote.php you just decode the query and use it. The standart PHP functions are base64_encode and base64_decode.
Did you try to encode URI using rawurlencode?
$url = rawurlencode('http://myfullwebsite.com/email/quote.php?fullname=Dan Smith&address=12 Main Street, London&emailaddress=dan#smith.com');

Get Request to an API url

I have signed up to a synonym API.. see the details on this page
I am having trouble implementing this in my php code.
If I copy and paste the link into the web browser, I can see the results no problem.
Instead of typing the word in manually, I wish to have a variable in the link with the relevant word i.e. $variable_with_word_stored as shown below.
http://words.bighugelabs.com/api/2/xxxxxxxx/$variable_with_word_stored/php
//format could be php (I would unserialize)..or json..I could decode it?
Any ideas guys? Thanks.
It sounds like you mean you want the result from calling that webpage and store it in a variable. What you should be looking to do is sending a http get request to that page within the code.
Check out using curl with php, you can send a http request to your requested url, capture the result back and parse it through json_decode
http://php.net/manual/en/curl.examples-basic.php
try it like this, maybe that you dont need curl:
$key = "xxxxxxxx";
$word = "love";
echo file_get_contents("http://words.bighugelabs.com/api/2/$key/$word/php");

Retrieve content of another website

I want to get the content of another page. The background is that I wanted to make an AJAX request but due to the Same Origin Policy I cannot do this. Now I wanted to write an own PHP script on which I make the AJAX request. The URL looks like the following:
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332
I tried it with fopen, curl and file_get_contents. Nothing from the works. The problem is if I put in the URL as string like
$results = file_get_contents('http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332');
it does work. If I put in a variable
$url = 'http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332';
$results = file_get_contents($url);
I come to a wrong page. With the specific parameter I get a result. If the parameter are not given correctly it seems that I come to a default page. I can't make a sense out of it.
The same for curl:
$curlSession = curl_init();
$options = array
(
CURLOPT_URL=>$url,
CURLOPT_HEADER=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true
);
curl_setopt_array($curlSession,$options);
$results = curl_exec($curlSession);
This doesn't work. If I put in the URL as string and not as variable I get some results! I thought that the ampersand & or the square brackets [] are the problem but I cannot say this. & should be reserved and [] are no correct URL parameters. But why does the direct input work and not the variable?
I used the variable because I make some replacements with str_replace where I make the query more flexible.
I saw similar questions here (cURL function not working, curl_setopt doesnt work with url as a variable) but there was never posted a real solution.
You have a , instead of a ; in your second code block.
Are you required to be "logged in" to the site that you're visiting? That would explain why it's working in your browser and not through your server script.
If all else is the same, your browser and the PHP functions you listed should return the same results.
Could you provide the actual URL for us to test?
EDIT: Based on the URL you provided, it's working fine for me:
php > $test = file_get_contents("http://www.domain.com/user/user_neu/index.php?id=16518&tx_stusermanager_pi9%5Bindications%5D=1&tx_stusermanager_pi9%5Bcategory%5D=cure&tx_stusermanager_pi9%5Baction%5D=getHousesByIndications&tx_stusermanager_pi9%5Bcontroller%5D=HouseFinder&cHash=88230660f01ads34d73a199b82e976");
php > var_dump($test);
string(29) "16,15,14,13,12,11,17,19,22"
My problem was that I used an encoded URL as starting point. E.g.
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9%5Bparameter%5D=%23%23%23param1%23%23%23&tx_manager_pi9%5Bcategory%5D=%23%23%23param2%23%23%23&tx_manager_pi9%5Baction%5D=getInfos&tx_manager_pi9%5Bcontroller%5D=Finder&cHash=123456789001233455332
I made a str_replace on a URL encoded string. Even using urldecode afterwards the URL was not correctly generated for curl, file_get_contents, ...
The correct URL should be something like this
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=###param1###&tx_manager_pi9[category]=###param2###&&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&&cHash=123456789001233455332
i.e. without &, %23, %5B, %5D

Simple $_GET question

Hey, i am currently creating a pagination script using php and jquery and for some reason can't display the $_GET that im trying to grab.
There is no form, but looking at the firebug console I get a url like this:
pagination.php?id=category
Just wondering how I can get that 'category' and just echo it out.
doesn't $_GET['id'] do it?
Try:
echo $_GET['id'];
Just in case that "category" is a URL, try to encode it, otherwise it will not populate correctly; and finally, don't use variables over 100 characters in length.
echo $_GET['id'];
also, be sure to sanitize user input before doing anything with it.

Categories