问题
Here's the script I'm trying to run:
<?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;
?>
It doesn't echo anything.
It's utilizing the MapQuest and Yahoo API (though I know that the problem is at the MapQuest API bit, because if I remove the MapQuest bit and echo it, it works). The API key is correct, btw.
$_GET[lat] is 33.8524925 and $_GET[long] is -117.73242200000001 .
回答1:
The call to MapquestAPI does return the following document:
<?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>
As you can see at the end, the <route />
element is empty. That is why
echo $mysongs4->route[0]->time;
Is empty, too. If it would be different you would have a real problem.
If you expect there to have something, contact the API vendor for your support options.
Additionally I suggest you, that you create variables for the URLs so that you can debug which URLs you are actually requesting. Not that you have an error in building the URIs. Take care.
Turns out that this is actually your issue. You need to request the API with the correct URI otherwise it does not work. Double check that you remove any spaces for the lat,long
pairs, the correct URI must look like:
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
Using that with simplexml_load_file
works fine, see the example:
$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();
Output:
<?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|¢er=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|¢er=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|¢er=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|¢er=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|¢er=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|¢er=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>
来源:https://stackoverflow.com/questions/14014619/simplexml-not-returning-anything