【问题标题】:SimpleXML not returning anythingSimpleXML 不返回任何内容
【发布时间】:2012-12-23 20:52:51
【问题描述】:

这是我要运行的脚本:

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

【问题讨论】:

    标签: php simplexml


    【解决方案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>
    

    正如你在最后看到的,&lt;route /&gt; 元素是空的。这就是为什么

    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>
    

    【讨论】:

    • 根据 MapQuest API,当调用 API 的代码中出现错误时,会传递“403”错误。我想知道代码不正确的地方。当我将要解析的 URL 粘贴到浏览器中时,它会返回方向和时间。感谢您的帮助...
    • @DumbProducts:错误是微不足道的:当您为请求构建 URI(将其放入变量中以便您可以调试它!)时,您在其中添加了空格时犯了一些小错误。我添加了一个工作 URI 的示例(来自您的示例数据)。构建 URI 时要小心,你很好。
    • 是的,仔细检查!有用!这么简单的东西,我却找不到……嗯,这就是为什么有像你这样的超级乐于助人的人,再次感谢!
    • 使用变量。当某些事情不起作用时,请检查是否满足您的期望。你可以看到我使用了变量并且我使用echo $...-&gt;asXML(); 来验证事情(我还使用了像 Xdebug 这样的调试器,所以我可以在任何地方停止执行并查看每个变量)。
    • 是的,print_rvar_dump 也适用于调试。某种裸机调试:) Xdebug 在这里:xdebug.org - 我可以建议将它与 Phpstorm 一起使用,一个完整的 IDE,有 30 天试用期:jetbrains.com/phpstorm
    猜你喜欢
    • 2020-04-27
    • 2019-04-06
    • 2012-02-15
    • 2020-09-07
    • 2021-11-22
    • 2015-11-06
    • 2013-11-14
    • 2014-03-23
    • 2012-08-25
    相关资源
    最近更新 更多