【问题标题】:Use PHP and cURL to pull JSON data from mixed text / html / javascript file使用 PHP 和 cURL 从混合文本/html/javascript 文件中提取 JSON 数据
【发布时间】:2015-11-22 11:04:00
【问题描述】:

THIS 链接上使用 PHP 和 cURL,返回一个包含类似以下信息的文件:

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <script>
            window['flyerData'] = {
                "id":489640,
                "categories":[{
                    "id":527,
                    "flyer_category_id":1201344,
                    "run_category_id":null,
                    "skipped":null,
                    "name":"Pharmacy",
                    "left":2925.0,
                    "bottom":-2560.0,
                    "right":4388.0,
                    "top":0.0,
                    "thumbnail_image_url":null
                }]
            }
        </script>
    </body>
</html>

如您所见,结果是混合的 html / javascript。我想做的是使用window['flyerData'],这样我就可以根据需要过滤值。

如何使用 PHP 和 cURL 来解决这个问题?

【问题讨论】:

  • 您使用 cURL 请求的文件在您的控制之下?你能以任何方式修改它吗?
  • 您是否有权在您的服务器上创建和执行 Node.JS 脚本?
  • @Alvaro Gonzalez 不,文件不在我的控制之下,这是一个刮擦。
  • @TbWill4321 我还不熟悉 Node.JS..。你想到了什么?

标签: javascript php json curl


【解决方案1】:

这非常适合拉我想要的字符串:

preg_match('/window\[\'flyerData\'\] \= (\{.*\};)/', $responseBody, $matches);
echo $matches[1];

【讨论】:

    【解决方案2】:

    你可以这样做(未经测试):

    <?php
        //get the contents of the curl call
        $curlOutput = "<!DOCTYPE html>
                            <html>
                                <head></head>
                                <body>
                                    <script>
                                        window['flyerData'] = {
                                            "id":489640,
                                            "categories":[{
                                                "id":527,
                                                "flyer_category_id":1201344,
                                                "run_category_id":null,
                                                "skipped":null,
                                                "name":"Pharmacy",
                                                "left":2925.0,
                                                "bottom":-2560.0,
                                                "right":4388.0,
                                                "top":0.0,
                                                "thumbnail_image_url":null
                                            }]
                                        }
                                    </script>
                                </body>
                            </html>";
        //strip out everything except for the values between the first '{' and the last '}'
        $json = substr($curlOutput, stripos($curlOutput, '{'), strripos($curlOutput, '}'));
        //parse that string as JSON
        $decodedJson = json_decode($json);
        var_dump(decodedJson);
        var_dump(decodedJson.categories);
    ?>
    

    但请注意,这种类型的解析被认为是脆弱的,因为来自curl 调用的字符串格式不能保证继续符合它现在所做的 HTML/JS。这就是为什么如果您可以访问一个定义良好的 API 是更好的选择。

    【讨论】:

    • 可能比这复杂一点。结果中有许多脚本标签。您将查看是否查看源代码。我知道window['flyerData'] 每次都是不变的,这就是我需要处理的全部内容。
    • 您的复杂性仅在于解析字符串以获得您想要的值。您可以将起始位置更改为字符串window['flyerData'] = { 的索引,然后查看该索引与第一次出现&lt;/script&gt; 之间的字符串。我敢肯定有很多正则表达式示例可以帮助您。
    • 您的解决方案很接近,但您的第二个 stripos 返回了在整个文件中找到的第一个 "}" 的索引,而不是在我正在搜索的上下文中找到的第一个。原来有在文件的前面已经有一个"}"。这导致最终输出被缩短。如果它是整个文件中唯一的"}",那么您的解决方案将是完美的,所以我会给你一个观点;)我最终使用了这个:preg_match('/window\[\'flyerData\'\] = (\{.*\}\;)/', $responseBody, $matches); echo $matches[1]; 哪个有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 2021-06-06
    • 2010-10-08
    • 2011-07-28
    • 1970-01-01
    • 2014-07-21
    相关资源
    最近更新 更多