在美国找不到城市名单



我正在努力获得一系列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);

有没有人有任何想法,我如何才能得到一个可靠的可用区域列表?

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

城市列表以JSON字符串的形式存储在其中一个javascript文件中,该列表实际上是由另一个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对象的结构,这将不起作用。

在任何情况下,根据他们的修改,重新工作可能是微不足道的,但这是一个潜在的问题。他们可能也不太可能做出这些逻辑上的改变,因为这需要修改许多文件,然后需要更多的测试。

希望有帮助!

可能有点晚了,但是您不需要耦合到我们的JavaScript来获取城市列表。我们有一个API:

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

最新更新