What is the most efficient way to get a part of a remote XML file via PHP?

前端 未结 2 765
粉色の甜心
粉色の甜心 2021-01-19 05:31

I\'m trying to get a portion of a remote XML file, in this case longitude and latitude for a zip code via Google Maps. This is the function I am currently using:

<         


        
2条回答
  •  没有蜡笔的小新
    2021-01-19 06:17

    Unless the service provides this functionality you can't request for a partial response based on a selector (e.g. XPath / CSS).

    Regarding efficiency, I would recommend breaking down the request time as detailed as possible to figure out why a certain request takes a while. cURL has pretty good support for this, e.g.:

    $zip = urlencode('1 infinite loop');
    $ch = curl_init("http://maps.googleapis.com/maps/api/geocode/xml?address={$zip}&sensor=false");
    curl_exec($ch);
    print_r(curl_getinfo($ch));
    

    This yields an array with all the meta data of your request:

    Array
    (
        ...
        [total_time] => 0.11955
        [namelookup_time] => 0.02996
        [connect_time] => 0.035803
        [pretransfer_time] => 0.035874
        ...
        [size_upload] => 0
        [size_download] => 1737
        [speed_download] => 14529
        [speed_upload] => 0
        [download_content_length] => -1
        [upload_content_length] => 0
        [starttransfer_time] => 0.119444
        [redirect_time] => 0
        ...
    )
    

    With this data you can craft the appropriate approach to handle the issue. Caching the responses may alleviate the request times for oft repeated searches, but it may not work for your particular case.

提交回复
热议问题