【问题标题】:CURL return data handlingCURL 返回数据处理
【发布时间】:2012-06-13 07:45:45
【问题描述】:

我编写了一个 PHP 函数来获取 URL 的加号

function makeApiCall($destinationUrl, $stringOfParams){
  $curl = curl_init();
  echo $destinationUrl.$stringOfParams."<br>";
  curl_setopt($curl, CURLOPT_URL, $destinationUrl.$stringOfParams);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $result = curl_exec($curl);
  curl_close($curl);
  echo $result;
 }

在输入 https://plusone.google.com/u/0/_/+1/fastbutton 作为目标 URL 并输入正确的参数字符串时,我在 $result 中收到的结果是 HTML。 问题是我想使用 PHP 来获取计数,而不是使用 JavaScript。

我该怎么做?

【问题讨论】:

    标签: php curl google-plus


    【解决方案1】:

    使用preg_match,可以实现。

    假设您正在调用这样的网址:

    https://plusone.google.com/_/+1/fastbutton?bsv=pr&url=http://www.google.com
    

    您正在寻找:

    <div id="aggregateCount" class="t1">118k</div>
    

    <div id="aggregateCount" class="t1">12</div>
    

    所以你可以执行:

    preg_match('/\<div id=\"aggregateCount\" class=\"t1\"\>\>?([0-9]*k?)\<\/div\>/i', $result, $matches);
    

    $matches 将是:

    Array
    (
        [0] => <div id="aggregateCount" class="t1">118k</div>
        [1] => 118k
    )
    

    编辑:

    运行示例后,似乎Google在使用curl时返回了不同的数字,例如在http://www.google.com上,它返回:

    <div id="aggregateCount" class="t1">>9999</div>
    

    所以我更新了正则表达式来处理&gt;

    【讨论】:

    猜你喜欢
    • 2013-09-22
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多