无法从LS获取城市列表

时间:2011-09-25 20:52:42

标签: php curl

我正在努力获得一系列LS城市... file_get_contents()会在路障上返回一个空的下拉列表,要求您选择城市。不幸的是它是空的......所以我认为它来自ajax请求。但是看看页面我在页面上看不到任何ajax请求。然后我尝试了CURL,认为可能模拟浏览器会有所帮助......下面的代码没有任何影响。

$ch = curl_init("http://www.URL.com/");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
$result=curl_exec($ch);
var_dump($result);

有没有人对如何获得可用区域的可靠列表有任何想法?

2 个答案:

答案 0 :(得分:2)

我已经了解了他们如何填充城市列表并创建了一些您可以使用的示例代码。

城市列表在其中一个javascript文件中存储为JSON字符串,该列表实际上是从不同的javascript文件填充的。文件的名称似乎有点随机,但根名称保持不变。

带有城市JSON的JS文件的示例是hXXp://a3.ak.lscdn.net/deals/system/javascripts/bingy-81bf24c3431bcffd317457ce1n434ca9.js填充列表的脚本是hXXp:// a2。 ak.lscdn.net/deals/system/javascripts/confirm_city-81bf24c3431bcffd317457ce1n434ca9.js但对我们来说这是无关紧要的。

我们需要使用新的curl会话加载他们的主页,查找唯一的javascript URL,这是一个bingy脚本并使用curl获取它。然后我们需要找到JSON并将其解码为PHP,以便我们可以使用它。

以下是我提出的适用于我的脚本:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);  // debugging

// set up new curl session with options
$ch = curl_init('http://livingsocial.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13');

$res = curl_exec($ch); // fetch home page

// regex string to find the bingy javascript file
$matchStr = '/src="(https?:\/\/.*?(?:javascripts)\/bingy-?[^\.]*\.js)"/i';

if (!preg_match($matchStr, $res, $bingyMatch)) {
    die('Failed to extract URL of javascript file!');
}

// this js file is now our new url
$url = $bingyMatch[1];

curl_setopt($ch, CURLOPT_URL, $url);

$res = curl_exec($ch); // fetch bingy js

$pos = strpos($res, 'fte_cities'); // search for the fte_cities variable where the list is stored

if ($pos === false) {
    die('Failed to locate cities JSON in javascript file!');
}

// find the beginning of the json string, and the end of the line
$startPos = strpos($res, '{', $pos + 1);
$endPos   = strpos($res, "\n", $pos + 1);

$json = trim(substr($res, $startPos, $endPos - $startPos)); // snip out the json

if (substr($json, -1) == ';') $json = substr($json, 0, -1); // remove trailing semicolon if present

$places = json_decode($json, true); // decode json to php array

if ($places == null) {
    die('Failed to decode JSON string of cities!');
}

// array is structured where each country is a key, and the value is an array of cities
foreach($places as $country => $cities) {
    echo "Country: $country<br />\n";

    foreach($cities as $city) {
        echo '  '
            ."{$city['name']} - {$city['id']}<br />\n";
    }

    echo "<br />\n";
}

一些重要的注释:

如果他们决定更改javascript文件名,则无法使用。 如果他们重命名包含城市的变量名称,则无法工作。 如果他们修改json以跨越多行,这将不起作用(这不太可能,因为它使用额外的带宽) 如果他们改变了json对象的结构,这将不起作用。

在任何情况下,根据他们的修改,再次工作可能是微不足道的,但这是一个潜在的问题。他们也可能不太可能进行这些后勤更改,因为它需要修改大量文件,然后需要进行更多测试。

希望有所帮助!

答案 1 :(得分:0)

也许有点晚了,但你不需要耦合我们的JavaScript来获取城市列表。我们有一个API:

https://sites.google.com/a/hungrymachine.com/livingsocial-api/home/cities