Tôi đồng ý với áp phích ban đầu của câu hỏi. Rất cụ thể:
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=301+E.+Linwood+Avenue++Turlock%2C+CA
Điều này đòi hỏi các cảm biến = biến giả để được thông qua, hoặc truy vấn sẽ trả lại kết quả BAD từ Google. Nếu tôi vượt qua STRING này thông qua đào tạo Quản lý, nó (file_get_contents PHP) thay thế "&" với "&"
do đó Google không thích tôi:
Array
(
[type] => 2
[message] => file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=301 E. Linwood Avenue Turlock, CA&sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
)
Vì vậy, đây là giải pháp tôi đã đưa ra, sử dụng http_build_query
$myURL = 'http://maps.googleapis.com/maps/api/geocode/json?';
$options = array("address"=>$myAddress,"sensor"=>"false");
$myURL .= http_build_query($options,'','&');
$myData = file_get_contents($myURL) or die(print_r(error_get_last()));
tôi cũng bao gồm mã (nhờ Marco K.) tôi tìm thấy trên các PHP website sử dụng một chức năng tùy chỉnh cho PHP < 5:
if (!function_exists('http_build_query')) {
function http_build_query($data, $prefix='', $sep='', $key='') {
$ret = array();
foreach ((array)$data as $k => $v) {
if (is_int($k) && $prefix != null) {
$k = urlencode($prefix . $k);
}
if ((!empty($key)) || ($key === 0)) $k = $key.'['.urlencode($k).']';
if (is_array($v) || is_object($v)) {
array_push($ret, http_build_query($v, '', $sep, $k));
} else {
array_push($ret, $k.'='.urlencode($v));
}
}
if (empty($sep)) $sep = ini_get('arg_separator.output');
return implode($sep, $ret);
}// http_build_query
}//if
bạn có thể chỉ cho một ít mã hơn xung quanh 'file_get_content() 'dòng, tôi nghi ngờ lỗi có thể nằm trong dòng trước đó. – gnarf
Điều này cũng đang xảy ra với tôi !! đây có phải là cronjob trên godaddy không? –
cùng một điều xảy ra với tôi, trong số 3 url chỉ có một là sản xuất & vì vậy không có cách nào để nói sử dụng curl, tôi muốn giải pháp trong chức năng này. file_get_contents() .. cảm ơn bạn – MFarooqi