Parse error: syntax error, unexpected T_STRING in

2019-01-26 19:46发布

<?php
    include_once 'forecastVo.php';
    include_once 'BaseVo.php';
    $count=0;
    $json_url = file_get_contents(
        'http://maps.google.com/maps/api/geocode/json' .
        '?address='jaipur'&sensor=false');                //line 9
    if($json_url){
        $obj = json_decode($json_url,true);

        $obj2= $obj['results'];
    }
?>

I am getting an error:

Parse error: syntax error, unexpected T_STRING in /home/a4101275/public_html/index.php on line 9

line 9 is where I am using the file_get_contents.

What does the error mean and how do I fix it?

标签: php parsing
3条回答
可以哭但决不认输i
2楼-- · 2019-01-26 20:20
$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address='jaipur'&sensor=false");

Or escaping it with \

查看更多
贪生不怕死
3楼-- · 2019-01-26 20:27

You have to use your escape characters correctly. You can't have a single-quote (') inside of a single-quote-encapsulated string. It breaks it. In order to continue the string and have PHP interpret your inner single-quote literally, you have to escape it with \.

$json_url = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=\'jaipur\'&sensor=false'); 

Or you can use the alternative string encapsulator, double-quote (").

$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address='jaipur'&sensor=false");

For future reference, Parse error: syntax error, unexpected T_STRING usually means you have a bad string somewhere on that line.

查看更多
Juvenile、少年°
4楼-- · 2019-01-26 20:27

Why quote it at all? I can't imagine the Google API requires (or even expects) that value to be quoted.

$json_url = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=jaipur&sensor=false'); //line 9

Or, is jaipur a variable? If so:

$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$jaipur&sensor=false"); //line 9

Hard to tell from your question what it is you're trying to accomplish...

查看更多
登录 后发表回答