简体   繁体   中英

Odd php file_get_contents behavior

I have written a file in php that is doing some behind the scenes work and returns a simple number based on the location GET parameter, in this case the output is 49, see for yourself, cut and paste this into your browser:

http://www.ericseven.com/distance.php?location=3133 W. Belle Ave San Tan Valley, AZ 85142

When I pull this into another php file, which I want to use the number to do some calculations with, it returns, inexplicably, a different number (in this case 7174, as you can see if you hit the following URL):

http://www.ericseven.com/test.php

This is the source of test.php - note that the $url is the same as the location parameter above (it is a cut and paste):

$url = "http://www.ericseven.com/distance.php?location=3133 W. Belle Ave San Tan Valley, AZ 85142";
$contents = file_get_contents($url);
echo $contents;

I have searched and searched and I don't know if I'm searching wrong or what the deal is but I can't find any relationship between the two numbers (49 and 7174) nor can I find anyone who is dealing with anything similar. FYI I tried fopen/fread - same results.

尝试

$url = "http://www.ericseven.com/distance.php?location=".urlencode("3133 W. Belle Ave San Tan Valley, AZ 85142");

You need to encode the URL.

<?php
$location = "3133 W. Belle Ave San Tan Valley, AZ 85142";
$url = 'http://www.ericseven.com/distance.php?location='.urlencode($location);
$contents = file_get_contents($url);
echo $contents;
?>

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