php script encoding special characters causes error - php

I'm attempting to run the script referenced here
<?php
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
echo "Variables:\n";
print_r($vars);
$id = reset(explode(':', $vars['id']));
echo "The id is $id\n";
$id = intval($vars['id']);
echo "Again, the id is $id\n";
Unlike the example shown - which works - on my station, the variable array shows that "&" is encoded to "amp;" causing that script not to work for me.
When I output the variable array from the example, I get variables like [amp;id]
How can that scriptbe modified with the "&" decoded so it will work on my station?
Thanks for your help

simple solution is
$url = html_entity_decode('index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44');

Related

How to use echo in url

i am trying to use echo inside url. i have store data from the form in database and now i am also fetching it on my page and its working well. Now i am trying to print that data i.e. number and date in url.
Is it possible and if possible please help me out
here is my data that i am fetching and it prints the output
echo $number;
echo $yyyymmdd;
and here is my url in which i want to insert ' echo $number; ' and ' echo $yyyymmdd; ' on the place of and .
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/<number>/date/<yyyymmdd>/");
I have also tried something like this but it gives error of syntex error.
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/"echo $number;"/date/"echo $yyyymmdd;"/");
Another way to add changing parameters to a URL (or string) is by using sprintf(). You define your URL and a type specifier like %d as a placeholder for numbers, and %s for strings. See the php doc for the full list of type specifiers.
$urlFormat = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/%d/date/%s/"
^ ^
Then call sprintf with the changing parameters in order of appearance.
$url = sprintf($urlFormat, $number, $yyyymmdd);
$json = file_get_contents($url);
This becomes more convenient especially if you are calling file get contents in a loop.
Create two variables and append those two inside double-quote or single quote, depending upon the quotes which you have opened and close it.
<?php
$number=123;
$yyyymmdd='2018-10-9';
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/".$number."/<number>/date/<".$yyyymmdd.">/");
?>
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/");
When you compose text, you do not need "echo" but just can write variable.
You can directly use variables in double quotes like this
file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/");
Sample code below
$number = 344;
$yyyymmdd = "20180301";
$url1 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/";
echo "url1 ".$url1."\n";
$url2 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/";
echo "url2 ".$url2. "\n";

Read classic ASP's cookies with PHP

How do I obtain the asp cookie's name and value using PHP so i may assign it to a variable? PHP and ASP are on the same domain.
Classic Asp Create Cookie
response.cookies("apple")("red")="reddelicious"
response.cookies("apple")("yellow")="gingergold"
response.cookies("apple")("green")="grannysmith"
response.cookies("apple").expires = dateadd("d",2,Now())
Classic ASP Read Cookie
strRed = request.cookies("apple")("red")
strYellow = request.cookies("apple")("yellow")
strGreen = request.cookies("apple")("green")
Reading The ASP cookies with PHP echo
echo $_COOKIE["apple"];
In firebug, after expanding the 'apple' cookie within the console, 'echo $_COOKIE["apple"]' outputs:
red=reddelicious&yellow=gingergold&green=grannysmith
Tried:
$strRed = $_COOKIE["apple"]["red"]; //doesn't work
You could use the parse_str function in php
<?php
parse_str($_COOKIE["apple"], $apple);
echo($apple["red"]);
?>
To get the string red=reddelicious&yellow=gingergold&green=grannysmith to the format of a multi dimensional array try this:
$itemArray = explode('&', $_COOKIE['apple']); // Split variables at '&'
foreach($itemArray as $item){ // for each variable pair 'key=value'
$itemParts = explode('=', $item); // split string to '[key, value]'
$apple[$itemParts[0]] = $itemParts[1]; // set each item to $apple[key] = value;
}
Then you can use the variable like this:
$strRed = $apple["red"]; //should work :)

How can I sanitise the explode() function to extract only the marker I require?

I have some php code that extracts a web address. The object I have extracted is of the form:
WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults
Now in PHP I have called this object $linkHREF
I want to extract the id element only and put it into an array (I'm bootstrapping this process to get multiple id's)
So the command is:
$detailPagePathArray = explode("id=",$linkHREF); #Array
Now the problem is the output of this includes what comes after the id tag, so the output looks like:
echo $detailPagePathArray[0] = WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&w
echo $detailPagePathArray[1] = bf&page=1&
echo $detailPagePathArray[2] = 16123012&source=searchresults
Now the problem is obvious, where it'd firstly picking up the "id" in the "wid" marker and cutting it there, however the secondary problem is it's also picking up all the material after the actual "id". I'm just interested in picking up "16123012".
Can you please explain how I can modify my explode command to point it to the particular marker I'm interested in?
Thanks.
Use the built-in functions provided for the purpose.
For example:
<?php
$url = 'http://www.example.com?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults';
$qs = parse_url($url);
parse_str($qs['query'], $vars);
$id = $vars['id'];
echo $id; // 16123012
?>
References:
parse_url()
parse_str()
if you are sure that you are getting &id=123456 only once in your object, then below
$linkHREF = "WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults";
$str = current(explode('&',end(explode('&id', $linkHREF,2))));
echo "id" .$str; //output id = 16123012

display + sign from the query string url using php

Im trying to send a query string in url
for ex : url : localhost/myfile.php?number=8777,+9822,+9883
in myfile.php when i give echo the query string :
echo $_REQUEST['number'];
output :
8777,9822,9883
but the expected output is :
8777,+9822,+9883
How can i display + sign also.
UPDATE :
actually that url is web request from the android/ios device app,
im providing webservice in php,
so android/ios developers are sending request with a querystring contains + sign
so how can i handle this situation?
+ is reserved. PHP is correct in translating an unencoded + sign to a space.
You can use urlencode() urldecode() for this.
The + must be submitted in PHP as the encoded value: %2B
You should then use urlencode() function to create that url:
<?php
var_dump($_GET['number']);
echo 'http://localhost/myfile.php?number='.urlencode('8777,+9822,+9883');
EDIT
If this is url that you receiving and cannot do anything with that you can use for example:
echo substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'=')+1);
and you will get
8777,+9822,+9883
Sorry, i dont know what you're trying to do, but here's a suggestion
// where base64_encode('8777,+9822,+9883') = ODc3NywrOTgyMiwrOTg4Mw
localhost/myfile.php?number=ODc3NywrOTgyMiwrOTg4Mw
// on myfile.php
echo base64_decode($_REQUEST['number']);
// this will output -> 8777,+9822,+9883
UPDATE ---------------------------
if you have no other choice , you can use this
<?php
// get URL query string
$params = $_SERVER['QUERY_STRING'];
// if you have $params = www.mydomain.com/myfile.php?number=9988,+9876,+8768
$temp = explode('=', $params);
echo $temp[1] .'<hr>';
// if you have $params = www.mydomain.com/myfile.php?number=9988,+9876,+8768&number2=123,+456,+789
$params2 = $_SERVER['QUERY_STRING'];
$temp3 = explode('&', $params2);
foreach($temp3 as $val){
$temp4 = explode('=', $val);
// # // $GET = $temp4[0]; // if you need the GET VALUES
$VALUE = $temp4[1];
echo $VALUE .'<br>';
}
?>
Hope this helps.... :)

PHP and JSON - Decoded Array Issue?

I'm trying to get some info out of a decoded JSon string into an array.
I've this code:
$json_d='{ // This is just an example, I normally get this from a request...
"iklive.com":{"status":"regthroughothers","classkey":"domcno"}
}';
$json_a=json_decode($json_d,true);
$full_domain = $domain.$tlds; // $domain = 'iklive' ; $tlds = '.com'
echo $json_a[$full_domain][status];
The problem is, I need to get the value for "status" of "iklive.com" but when I do echo $json_a[$full_domain][status]; it does not work, but if I do it manually like echo $json_a['iklive.com'][status]; (with the quotes there) it works.
I've tried to add the quotes to the variable but without success, how can I do this?
Thanks Everyone!
Thanks to Pekka and jeromegamez I noticed a error in the HTML part of this "problem", the $tlds variable was "com" instead of ".com" -- Sorry by wasting your time with this. I do feel bad now.
Anyway, thanks to jeromegamez and Marc B I discovered that unless status was a constant I need to quote it ;) You can check jeromegamez answer to a detailed explanation of the problem and proper debug.
Sorry.
This works for me:
<?php
$json_d='{ "iklive.com":{"status":"regthroughothers","classkey":"domcno"} }';
$json_a = json_decode($json_d, true);
if (!is_array($json_a)) {
echo "\$json_d is not a valid JSON array\n";
}
$domain = "iklive";
$tld = ".com";
$full_domain = $domain . $tld;
if (!isset($json_a[$full_domain])) {
echo "{$full_domain} is not set in \$json_a\n";
} else {
echo $json_a[$full_domain]['status']."\n";
}
What I did:
Changed json_a[$full_domain][status] to json_a[$full_domain]['status'] - the missing quotes around status don't break your script, but raise a Notice: Use of undefined constant status - assumed 'status'
Added a check if the decoded JSON is actually an array
Added a check whether the key $full_domain is set in $json_a

Categories