user1512672
user1512672

Reputation: 3

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.

Upvotes: 0

Views: 184

Answers (2)

meme
meme

Reputation: 12197

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;
?>

Upvotes: 2

Sabeen Malik
Sabeen Malik

Reputation: 10880

Try

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

Upvotes: 2

Related Questions