simple html dom loading different page than url

I'm trying to pull in a webpage with php's simple html dom parser and I'm having an odd problem with it. I am defining the url variable by concatenating two strings.

$itemUrl="http://www.example.com/";
$pageUrl="default.asp?pg=DispSingleItem&ItemNumber=1234";
$itemUrl .=$pageUrl;
$html=file_get_html($itemUrl);

When I run this script I end up going to a page that says "Item Not Found". However, when I echo the $itemUrl, copy the output, and paste it into the browser, I go directly to the page I am looking for. Also, if I just put the entire url saved as one string to begin with it, takes me to the correct webpage.

$itemUrl="http://www.example.com/default.asp?pg=DispSingleItem&ItemNumber=1234";
$html=file_get_html($itemUrl);

I have tried using urlencode on the concatenated string, but that doesn't help. I am really stumped. I can't think of a reason why the concatenated url would take me to a different page when the same url, copied directly and pasted into another variable, takes me to the correct page. What am I missing here? Any help would be great, thank you.

EDIT: I found the issue. The echo of $pageUrl showed this:

default.asp?pg=DispSingleItem&ItemNumber=1234

When in reality the variable was saving a string that looked like this:

default.asp?pg=DispSingleItem&ItemNumber=1234

My browser was automatically translating the & amp; to simply &, which looked like exactly what I needed. Whenever I ran file_get_html though, the string was not converted and so the script was trying to get it from a url that had & amp; not just &. I added:

$pageUrl=preg_replace("/amp;/", "", $pageUrl);

Does anyone know of a way to get a browser to output exactly what the variable is, without altering it like what happened in my problem? It would be useful to know in the future.

Thank you to all of the responses. I wasn't expecting so many or so quickly.

Answers


Your last question was:

Does anyone know of a way to get a browser to output exactly what the variable is, without altering it like what happened in my problem? It would be useful to know in the future.

If I understood it correctly, you would like to know about a way of preventing the browser of rendering a url string like www.example.com?p=1&e=2 as www.example.com?php=1&e=2.

A way of doing that is using echo htmlentities($url); instead of merely echo $url;. That way, the &amp part of the string would be rendered by the browser exactly as that.

Example code:

<?php
$url = "www.example.com?p=1&amp;e=2";
echo $url;
echo "<br/>";
echo htmlentities($url);
?>

Resulting browser output:

www.example.com?p=1&e=2
www.example.com?p=1&amp;e=2

Need Your Help

How to take an action after certain time (different from user to user)?

php javascript mysql cron

I'm developing a web game (js php mysql) in which one clicks a button to start an action that takes time to complete (let's say 10 hours) and when it finishes some points are added to that player's...