Using PHP Simple HTML DOM Parser url with php variable - php

I need to be able to include a variable to the url so when i use file_get_html i can get to the specific page i need.
Here is what i am trying as a test.
$activity= 'com.google.com';
$html = file_get_html("https://play.google.com/store/apps/details?id=".$activity) or die('this is not a valid url');
echo $html;
The activity is what will change when running my mysql_fetch_array. I have tried single quotes, double quotes no quotes and cannot come up with a solution.
I just get this is not a valid url
If i use this, it works because its replacing just the base url
$a = 'google.com';
$b = "http://" . $a . "/";
$html = file_get_html($b)
It seems to only be an issue when accessing a specific url
UPDATE
The above wasn't working because it was not a valid url. when entering a valid url, it work.
However, putting in my real world scenario it says not a valid url
$html = file_get_html("https://play.google.com/store/apps/details?id=".$row['activity']) or die('this is not a valid url');

i think its about the https of your first url seems like file_get_html having issues with validate certificates

Related

“&” being replaced by "& in php include

I have a php page which get response from another page as shown:
while($response!=200)
{
$response = include 'xyz.php?one='.$one.'&two='.$two.'&three='.$three.'';
}
But my link always get's something like:
domainname.com/xyz.php?one=content&two=content&three=content
And due to & getting replaced by & I am getting the page not found issue.
I have tried using %26 and directly putting & instead of &, all in vain.
Is there any other simple solution besides using string replace function of PHP to remove & and replace it with &
Check out html_entity_decode
$response = html_entity_decode($response)
I ran a test based on the code you sent and I don't have a problem. That suggests you have something auto-magical going on in your *.ini file (magic quotes, maybe... ugh...). Try to create the string simply as a variable to remove it from the filename context and echo it out to be sure it's right, then use the variable with your include.
$one = 'abc';
$two = 'def';
$three = "ghi";
$file= 'xyz.php?one='.$one.'&two='.$two.'&three='.$three;
echo "\n\n".$file;
$response = include $file;
You can't use URL parameters when accessing a local file, they have to go through the webserver. Try:
$response = file_get_contents("http://localhost/path/to/xyz.php?one='.$one.'&two='.$two.'&three='.$three);

Premium url shortner issue with urlencode replacing & sign with ampersand

As a novice and beginner php learner, I'm using the Code-Canyon Premium URL Shortner script and done 2 days of research. Unfortunately I am unable to resolve my issue.
The url shorten script is urlencoding the API url that it sends to the script, In doing this it is replacing the & symbols with & causing the url to not work correctly on the final destination page.
I have tried to use preg_replace, str_replace and also tried to use urldecode on the destination page but none of these seem to work. Here is my current script:
$makeshort = "http://mywebsite.com/email/quote.php?quoteid=$visitor&customertype=fhbs";
$mkshrt = str_replace("/&/","%26",$makeshort);
$short = "http://shorturl.com/api?&api=REMOVED&format=text&url=".urlencode($mkshrt);
// Using Plain Text Response
$api_url = $short;
$res= #file_get_contents($api_url);
if($res)
$shorturl = $res;
$shorty = json_decode($shorturl);
$shorturl = $shorty->{'short'};
echo $shorturl;
Note: Where you see &format=text in the api url, I have tried to use it with and without the &format=text however this makes no difference what so ever.
I am hoping that there could be a simple and quick way to resolve this issue as I am only passing over 2 variables and its the second variable that is being displayed like this:
mywebsite.com/email/quote.php?quoteid=01234567890&customertype=fhbs
So the customertype variable is the one being messed up due to the amp; symbol.
I sincerely hope someone with the expertise could advise me on the best approach or even a simple way to resolve this issues as I really am at my whits end! MY knowledge is not good enough to research the exact key phrases in order to point myself in the right direction.
Thanks for your time in reading this and I hope someone would be kind enough to help me out here.
I know the feeling as i myself am just becoming to terms with coding and developing.
I personally would solve this by one of two ways, If you have tried to already use htmlspecialchars or htmlentities along with urldecode then the most simple and quickest way to achieve this would be to read the URL string then replace the &symbol with the & using str_replace and do either a meta refresh of the page or `header location redirect
Here is what i mean with a breif example however one must stress that some extra security maybe needed and this is ONLY a quick fix not a secure stable and permanent fix, Though one could play with this and maybe work something out for your own circumstances.
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&")){
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url = str_replace('&', '&', $url);
echo "<meta http-equiv='refresh' content='0;URL=$url'>";
exit;
}
Alternative way with header location:
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&")){
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url = str_replace('&', '&', $url);
header("Location: $url");
exit();
}
This will totally remove any & symbols from the url and replace them with &.
You can also play around with this to remove even more from the url string and replace things like / or forbidden words.
An example of the output will look like this:
Original url causing the problems:
http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs
New url after the script has executed and refreshed the page:
http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs
As you can see from the hyperlinked text above, The ampersand breaks the string and everything after that is not read correctly but when this script executes and refreshes the page the url will be just like the second hyperlink thus making the url work for what you require.
NOTE: THIS IS NOT A SECURE WAY OF DOING THINGS AND MAY NO BE IDEAL FOR YOUR CIRCUMSTANCES, THIS IS JUST AN IDEA AND HOPE THIS HELPS!
Thanks.

How can I remove invalid querystring using php header location

I have this invalid link hard coded in software which I cannot modify.
http://www.16start.com/results.php?cof=GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search
I would like to use php header location to redirect it to a valid URL which does not contain the querystring. I'd like to pass just the parameter q=.
I've tried
$q = $_GET['q'];
header ("Location: http://www.newURL.com/results.php?" . $q . "");
But it's just passing the invalid querystring to the new location in addition to modifying it in a strange way
This is the destination location I get, which is also invalid
http://www.newURL.com/results.php?#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search
That's because # is seen as the start of a fragment identifier and confuses the parser.
You can take the easy-way as Stretch suggested but you should be aware that q is the last query parameter in your URL. Therefore, it might be better to fix the URL and extract the query parameters in a safer way:
<?php
$url = "http://www.16start.com/results.php?cof=GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search";
// Replace # with its HTML entity:
$url = str_replace('#', "%23", $url);
// Extract the query part from the URL
$query = parse_url($url, PHP_URL_QUERY);
// From here on you could prepend the new url
$newUrl = "http://www.newURL.com/results.php?" . $query;
var_dump($newUrl);
// Or you can even go further and convert the query part into an array
parse_str($query, $params);
var_dump($params);
?>
Output
string 'http://www.newURL.com/results.php?cof=GALT:%23FFFFFF;GL:1;DIV:%23FFFFFF;FORID:1&q=search' (length=88)
array
'cof' => string 'GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1' (length=37)
'q' => string 'search' (length=6)
Update
After your comments, it seems that the URL is not available as a string in your script and you want to get it from the browser.
The bad news is that PHP will not receive the fragment part (everything after the #), because it is not sent to the server. You can verify this if you check the network tab in the Development tools of your browser F12.
In this case, you'll have to host a page at http://www.16start.com/results.php that contains some client-side JavaScript for parsing the fragment and redirecting the user.
one way could be to use strstr() to get everything after (and including q=) in the string.
So:
$q=strstr($_GET['q'],'q=');
Give that a whirl

PHP not using full URL?

This is my code:
<?php
$url = "https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT?$filter=Kenteken%20eq%20%2701GBB1%27";
$xml = simplexml_load_file($url);
?>
Im trying to get this .XML file into my $url. The problem is that its using
https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT and not the one with the filter:
https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT?$filter=Kenteken%20eq%20%2701GBB1%27
Why is it not taking the URL i entered there? When i open the URL in my browser i get the filtered one but when i run the code itll give me the other .XML.
I hope someone can help me.
You are using double quotes, so php will interpret $filter as a (probably undefined...) variable.
You can avoid that using single quotes:
$url = 'https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT?$filter=Kenteken%20eq%20%2701GBB1%27';
Use single quotes.
Then re-read the docs
Have you tried this?
<?php
$url='https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT? $filter=Kenteken%20eq%20%2701GBB1%27';
$xml = new SimpleXMLElement(file_get_contents($url));
?>

$_GET, Ampersand is interrupting

Trying to use a $_GET['url'] variable to grab data from a URL:
http://mysite.com/?url=http://this.is/?q=an&?example=url
What I want above is bolded, but sadly the $_GET['url'] will only get "http:// this.is/?q=an" because the & makes it interpret it as the beginning of a new variable within the URL.
Is there a way to ignore the ampersands so my script can get the entire URL I need it to? The URL that is appended to ?url= is not within my limits to control so most work but some do contain the dreaded &. After reading questions on Stack Overflow I'm not holding out much hope :(
If you have absolutely no control over the arguments placed on the query string (for whatever reason), you can also do this by manually parsing the $_SERVER['QUERY_STRING'] varible, e.g.
$page = str_replace("url=", "", $_SERVER['QUERY_STRING']);
Of course, if possible, you should encode it using the answers posted by everyone else.
Use urlencode()
$get_url = urlencode('http://this.is/?q=an&?example=url');
$url = 'http://mysite.com/?url=' . $get_url;
If you can't control the query string, you could use
$query = $_SERVER['QUERY_STRING'];
$pos = strpos($query, "url=");
if ($pos !== false) {
$url = substr($query, $pos + 4);
}
This code returns everthing after url=
If you have the possibility,you should encode the url with urlencode to create a clean url.
If you don't do this, you get eventually an server error, with strange urls and you can't pass more than one url(because everything after the url= is interpreted as url)
Here is the tested code:
if (!empty($_GET['url'])) {
echo $_GET['url'].'<br />';
}
$url = urlencode('http://this.is/?q=an&?example=url');
echo 'LINK';
Hope it will help you.

Categories