0

这是我要运行的脚本:

    <?php
$lat = $_GET[lat];
$long = $_GET[long];
    $mysongs3 = simplexml_load_file('http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=845e721464167b9976ca031b0c3c66f3cc6563ee&query=restaurant&latitude='.$lat.'&longitude='.$long.'&results=2');
   $lat2 = $mysongs3->Result[0]->Latitude;
   $long2 = $mysongs3->Result[0]->Longitude;
   $mysongs4 = simplexml_load_file('http://www.mapquestapi.com/directions/v1/route?key=Fmjtd%7Cluuan1urn9%2Cb0%3Do5-968206&from='.$lat.', '.$long.'&to='.$lat2.', '.$long2.'&callback=renderNarrative&outFormat=xml');
echo $mysongs4->route[0]->time;
?>

它不回声任何东西。

它使用了 MapQuest 和 Yahoo API(尽管我知道问题出在 MapQuest API 位,因为如果我删除 MapQuest 位并回显它,它就可以工作)。顺便说一句,API 密钥是正确的。

$_GET[lat] 是 33.8524925 和 $_GET[long] 是 -117.73242200000001 。

4

1 回答 1

1

对 MapquestAPI 的调用确实会返回以下文档:

<?xml version = "1.0" encoding = "UTF-8" ?>
<response>
    <info>
        <statusCode>403</statusCode>
        <messages>
            <message/>
        </messages>
        <copyright>
            <imageUrl>http://api.mqcdn.com/res/mqlogo.gif</imageUrl>
            <imageAltText>© 2012 MapQuest, Inc.</imageAltText>
            <text>© 2012 MapQuest, Inc.</text>
        </copyright>
    </info>
    <route/>
</response>

正如你在最后看到的,<route />元素是空的。因此

echo $mysongs4->route[0]->time;

也是空的。如果情况不同,您将遇到真正的问题。

如果您希望有一些东西,请联系 API 供应商以获取您的支持选项。

此外,我建议您为 URL 创建变量,以便调试您实际请求的 URL。并不是说您在构建 URI 时出错。小心。


原来这实际上是你的问题。您需要使用正确的 URI 请求 API,否则它不起作用。仔细检查您是否删除了lat,long对的任何空格,正确的 URI 必须如下所示:

http://www.mapquestapi.com/directions/v1/route?key=Fmjtd%7Cluuan1urn9%2Cb0%3Do5-968206&from=33.8524925,-117.73242200000001&to=33.863084,-117.739601&callback=renderNarrative&outFormat=xml

使用它可以simplexml_load_file正常工作,请参见示例:

$mapqueryUri = 'http://www.mapquestapi.com/directions/v1/route?key=Fmjtd%7Cluuan1urn9%2Cb0%3Do5-968206' .
               '&from=33.8524925,-117.73242200000001&to=33.863084,-117.739601&callback=renderNarrative' .
               '&outFormat=xml';
$mysongs4    = simplexml_load_file($mapqueryUri);
echo $mysongs4->asXML();

输出:

<?xml version = "1.0" encoding = "UTF-8"?>
<response>
    <info>
        <statusCode>0</statusCode>
        <messages/>
        <copyright>
            <imageUrl>http://api.mqcdn.com/res/mqlogo.gif</imageUrl>
            <imageAltText>© 2012 MapQuest, Inc.</imageAltText>
            <text>© 2012 MapQuest, Inc.</text>
        </copyright>
    </info>
    <route>
        <sessionId>50d7729f-0223-0013-02b7-60f4-d485645ba5f2</sessionId>
        <options>
            <shapeFormat>raw</shapeFormat>
            <generalize>-1.0</generalize>
            <maxLinkId>0</maxLinkId>
            <narrativeType>text</narrativeType>
            <stateBoundaryDisplay>true</stateBoundaryDisplay>
            <countryBoundaryDisplay>true</countryBoundaryDisplay>
            <sideOfStreetDisplay>true</sideOfStreetDisplay>
            <destinationManeuverDisplay>true</destinationManeuverDisplay>
            <avoidTimedConditions>false</avoidTimedConditions>
            <enhancedNarrative>false</enhancedNarrative>
            <returnLinkDirections>false</returnLinkDirections>
            <timeType>0</timeType>
            <routeType>FASTEST</routeType>
            <locale>en_US</locale>
            <unit>M</unit>
            <tryAvoidLinkIds/>
            <mustAvoidLinkIds/>
            <manmaps>true</manmaps>
            <drivingStyle>2</drivingStyle>
            <highwayEfficiency>22.0</highwayEfficiency>
            <useTraffic>false</useTraffic>
        </options>
        <boundingBox>
            <ul>
                <lat>33.863009999999996</lat>
                <lng>-117.741813</lng>
            </ul>
            <lr>
                <lat>33.852661</lat>
                <lng>-117.732475</lng>
            </lr>
        </boundingBox>
        <distance>1.241</distance>
        <time>272</time>
        <realTime>272</realTime>
        <fuelUsed>0.089725</fuelUsed>
        <formattedTime>00:04:32</formattedTime>
        <legs>
            <leg>
                <distance>1.241</distance>
                <time>272</time>
                <formattedTime>00:04:32</formattedTime>
                <index>0</index>
                <maneuvers>
                    <maneuver>
                        <startPoint>
                            <lat>33.8528</lat>
                            <lng>-117.7325</lng>
                        </startPoint>
                        <maneuverNotes/>
                        <distance>0.155</distance>
                        <time>34</time>
                        <formattedTime>00:00:34</formattedTime>
                        <attributes>0</attributes>
                        <turnType>6</turnType>
                        <direction>7</direction>
                        <narrative>Start out going west on E Marblehead Way toward S Hanlon Way.</narrative>
                        <directionName>West</directionName>
                        <index>0</index>
                        <streets>
                            <street>E Marblehead Way</street>
                        </streets>
                        <signs/>
                        <iconUrl><![CDATA[http://content.mapquest.com/mqsite/turnsigns/rs_left_sm.gif]]></iconUrl>
                        <linkIds/>
                        <mapUrl>
                            <![CDATA[http://www.mapquestapi.com/staticmap/v4/getmap?type=map&size=225,160&pois=purple-2,33.852756,-117.732475,0,0|purple-3,33.853279,-117.73499199999999,0,0|&center=33.8530175,-117.7337335&zoom=13&key=Fmjtd|luuan1urn9,b0=o5-968206&rand=2931445&session=50d7729f-0223-0013-02b7-60f4-d485645ba5f2]]>
                        </mapUrl>
                    </maneuver>
                    <maneuver>
                        <startPoint>
                            <lat>33.8533</lat>
                            <lng>-117.735</lng>
                        </startPoint>
                        <maneuverNotes/>
                        <distance>0.521</distance>
                        <time>68</time>
                        <formattedTime>00:01:08</formattedTime>
                        <attributes>0</attributes>
                        <turnType>2</turnType>
                        <direction>2</direction>
                        <narrative>Turn right onto S Serrano Ave.</narrative>
                        <directionName>Northwest</directionName>
                        <index>1</index>
                        <streets>
                            <street>S Serrano Ave</street>
                        </streets>
                        <signs/>
                        <iconUrl><![CDATA[http://content.mapquest.com/mqsite/turnsigns/rs_right_sm.gif]]></iconUrl>
                        <linkIds/>
                        <mapUrl>
                            <![CDATA[http://www.mapquestapi.com/staticmap/v4/getmap?type=map&size=225,160&pois=purple-3,33.853279,-117.73499199999999,0,0|purple-4,33.860351,-117.737342,0,0|&center=33.856815,-117.736167&zoom=10&key=Fmjtd|luuan1urn9,b0=o5-968206&rand=2546696&session=50d7729f-0223-0013-02b7-60f4-d485645ba5f2]]>
                        </mapUrl>
                    </maneuver>
                    <maneuver>
                        <startPoint>
                            <lat>33.8604</lat>
                            <lng>-117.7373</lng>
                        </startPoint>
                        <maneuverNotes/>
                        <distance>0.199</distance>
                        <time>49</time>
                        <formattedTime>00:00:49</formattedTime>
                        <attributes>0</attributes>
                        <turnType>6</turnType>
                        <direction>7</direction>
                        <narrative>Turn left onto Canyon Creek Rd.</narrative>
                        <directionName>West</directionName>
                        <index>2</index>
                        <streets>
                            <street>Canyon Creek Rd</street>
                        </streets>
                        <signs/>
                        <iconUrl><![CDATA[http://content.mapquest.com/mqsite/turnsigns/rs_left_sm.gif]]></iconUrl>
                        <linkIds/>
                        <mapUrl>
                            <![CDATA[http://www.mapquestapi.com/staticmap/v4/getmap?type=map&size=225,160&pois=purple-4,33.860351,-117.737342,0,0|purple-5,33.860008,-117.740676,0,0|&center=33.8601795,-117.739009&zoom=13&key=Fmjtd|luuan1urn9,b0=o5-968206&rand=2546696&session=50d7729f-0223-0013-02b7-60f4-d485645ba5f2]]>
                        </mapUrl>
                    </maneuver>
                    <maneuver>
                        <startPoint>
                            <lat>33.86</lat>
                            <lng>-117.7407</lng>
                        </startPoint>
                        <maneuverNotes/>
                        <distance>0.163</distance>
                        <time>60</time>
                        <formattedTime>00:01:00</formattedTime>
                        <attributes>0</attributes>
                        <turnType>2</turnType>
                        <direction>1</direction>
                        <narrative>Turn right onto S Glenhurst Dr.</narrative>
                        <directionName>North</directionName>
                        <index>3</index>
                        <streets>
                            <street>S Glenhurst Dr</street>
                        </streets>
                        <signs/>
                        <iconUrl><![CDATA[http://content.mapquest.com/mqsite/turnsigns/rs_right_sm.gif]]></iconUrl>
                        <linkIds/>
                        <mapUrl>
                            <![CDATA[http://www.mapquestapi.com/staticmap/v4/getmap?type=map&size=225,160&pois=purple-5,33.860008,-117.740676,0,0|purple-6,33.862079,-117.741706,0,0|&center=33.8610435,-117.74119099999999&zoom=12&key=Fmjtd|luuan1urn9,b0=o5-968206&rand=2546696&session=50d7729f-0223-0013-02b7-60f4-d485645ba5f2]]>
                        </mapUrl>
                    </maneuver>
                    <maneuver>
                        <startPoint>
                            <lat>33.8621</lat>
                            <lng>-117.7417</lng>
                        </startPoint>
                        <maneuverNotes/>
                        <distance>0.044</distance>
                        <time>20</time>
                        <formattedTime>00:00:20</formattedTime>
                        <attributes>0</attributes>
                        <turnType>2</turnType>
                        <direction>3</direction>
                        <narrative>Turn right to stay on S Glenhurst Dr.</narrative>
                        <directionName>Northeast</directionName>
                        <index>4</index>
                        <streets>
                            <street>S Glenhurst Dr</street>
                        </streets>
                        <signs/>
                        <iconUrl><![CDATA[http://content.mapquest.com/mqsite/turnsigns/rs_right_sm.gif]]></iconUrl>
                        <linkIds/>
                        <mapUrl>
                            <![CDATA[http://www.mapquestapi.com/staticmap/v4/getmap?type=map&size=225,160&pois=purple-6,33.862079,-117.741706,0,0|purple-7,33.862358,-117.74101999999999,0,0|&center=33.8622185,-117.74136299999999&zoom=15&key=Fmjtd|luuan1urn9,b0=o5-968206&rand=2546696&session=50d7729f-0223-0013-02b7-60f4-d485645ba5f2]]>
                        </mapUrl>
                    </maneuver>
                    <maneuver>
                        <startPoint>
                            <lat>33.8624</lat>
                            <lng>-117.741</lng>
                        </startPoint>
                        <maneuverNotes/>
                        <distance>0.159</distance>
                        <time>41</time>
                        <formattedTime>00:00:41</formattedTime>
                        <attributes>0</attributes>
                        <turnType>2</turnType>
                        <direction>2</direction>
                        <narrative>Turn right onto E Oak Ridge Cir.</narrative>
                        <directionName>Northwest</directionName>
                        <index>5</index>
                        <streets>
                            <street>E Oak Ridge Cir</street>
                        </streets>
                        <signs/>
                        <iconUrl><![CDATA[http://content.mapquest.com/mqsite/turnsigns/rs_right_sm.gif]]></iconUrl>
                        <linkIds/>
                        <mapUrl>
                            <![CDATA[http://www.mapquestapi.com/staticmap/v4/getmap?type=map&size=225,160&pois=purple-7,33.862358,-117.74101999999999,0,0|purple-8,33.863009999999996,-117.739685,0,0|&center=33.862684,-117.7403525&zoom=14&key=Fmjtd|luuan1urn9,b0=o5-968206&rand=2546696&session=50d7729f-0223-0013-02b7-60f4-d485645ba5f2]]>
                        </mapUrl>
                    </maneuver>
                    <maneuver>
                        <startPoint/>
                        <maneuverNotes/>
                        <distance>0.0</distance>
                        <time>0</time>
                        <formattedTime>00:00:00</formattedTime>
                        <attributes>0</attributes>
                        <turnType>-1</turnType>
                        <direction>0</direction>
                        <narrative>8175 E OAK RIDGE CIR is on the right.</narrative>
                        <directionName/>
                        <index>6</index>
                        <streets/>
                        <signs/>
                        <linkIds/>
                        <mapUrl><![CDATA[]]></mapUrl>
                    </maneuver>
                </maneuvers>
                <hasTollRoad>false</hasTollRoad>
                <hasFerry>false</hasFerry>
                <hasHighway>false</hasHighway>
                <hasSeasonalClosure>false</hasSeasonalClosure>
                <hasUnpaved>false</hasUnpaved>
                <hasCountryCross>false</hasCountryCross>
            </leg>
        </legs>
        <hasTollRoad>false</hasTollRoad>
        <hasFerry>false</hasFerry>
        <hasHighway>false</hasHighway>
        <hasSeasonalClosure>false</hasSeasonalClosure>
        <hasUnpaved>false</hasUnpaved>
        <hasCountryCross>false</hasCountryCross>
        <locations>
            <location>
                <street>8250 E Marblehead Way</street>
                <adminArea5 type="City">Anaheim</adminArea5>
                <adminArea3 type="State">CA</adminArea3>
                <adminArea4 type="County">Orange</adminArea4>
                <postalCode>92808</postalCode>
                <adminArea1 type="Country">US</adminArea1>
                <geocodeQuality>ADDRESS</geocodeQuality>
                <geocodeQualityCode>L1AAA</geocodeQualityCode>
                <dragPoint>false</dragPoint>
                <sideOfStreet>L</sideOfStreet>
                <displayLatLng>
                    <latLng>
                        <lat>33.852492</lat>
                        <lng>-117.732422</lng>
                    </latLng>
                </displayLatLng>
                <linkId>24154164</linkId>
                <type>s</type>
                <latLng>
                    <lat>33.852492</lat>
                    <lng>-117.732422</lng>
                </latLng>
            </location>
            <location>
                <street>8175 E Oak Ridge Cir</street>
                <adminArea5 type="City">Anaheim</adminArea5>
                <adminArea3 type="State">CA</adminArea3>
                <adminArea4 type="County">Orange</adminArea4>
                <postalCode>92808</postalCode>
                <adminArea1 type="Country">US</adminArea1>
                <geocodeQuality>ADDRESS</geocodeQuality>
                <geocodeQualityCode>L1AAA</geocodeQualityCode>
                <dragPoint>false</dragPoint>
                <sideOfStreet>R</sideOfStreet>
                <displayLatLng>
                    <latLng>
                        <lat>33.863084</lat>
                        <lng>-117.739601</lng>
                    </latLng>
                </displayLatLng>
                <linkId>24115749</linkId>
                <type>s</type>
                <latLng>
                    <lat>33.863084</lat>
                    <lng>-117.739601</lng>
                </latLng>
            </location>
        </locations>
        <locationSequence>0,1</locationSequence>
        <computedWaypoints/>
    </route>
</response>
于 2012-12-23T21:04:03.943 回答