简体   繁体   中英

simple html dom and a ? in the url

When I try to use the following code:

$html = new simple_html_dom();  
$url = "http://www.example.com/changeCity.php?city=NewYork=";

$html->load_file($url);  

it loads http://www.example.com/changeCity.php .

How do I get it to load for NewYork city?

I know what the entire URL is, it's just that I don't know how to handle it when it has a ? in it.

** * ** * * EDIT * ** * ** * ***

I'm still not having much luck. The above code actually loads http://www.example.com/index.php - I made an error above. The changeCity.php?city=NewYork= should change the city to New York, however this will still be under the url http://www.example.com/index.php . All cities have the same url - http://www.example.com/index.php .

What's happening is that the code is indeed loading http://www.example.com/index.php but it always defaults to the default city, basically the change city doesn't work. I've tried for other cities as well, not just New York but the loaded url always defaults back to the default city. For some reason the code does not recognize the ? which redirects the url.

The trailing = doesn't make a difference, I've tried the code with and without the =.

Any ideas?

您是否尝试过对$ url进行urlencode?

$url = urlencode("http://www.example.com/changeCity.php?city=NewYork");

I'm not sure what simple_html_dom does, but what you could try:

$html = str_get_html(file_get_contents($url));

str_get_html is part of that PHP lib. Second tab ("How to modify HTML elements?") on http://simplehtmldom.sourceforge.net/

You should indeed remove or encode the trailing slash.

This will load changeCity.php, and pass NewYork= as a variable in the $_GET array. You can do something with that variable. In other words $_GET['city'] == NewYork= .

If you want to load a page that has a filename of NewYork, then you could do something like:

// changeCity.php
$page = $_GET['city'];
// secure $page variable
incude($page . '.php');

I'm pretty sure you understand this, but the OP is not entirely clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM