普通网友 2012-06-13 07:45
浏览 219
已采纳

CURL返回数据处理

I've written a PHP function to get plus ones count for a 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;
 }

While inputting https://plusone.google.com/u/0/_/+1/fastbutton as destination URL and inputting the correct string of params, the result I'm receiving in $result is HTML. The problem is that I would like using PHP to get the count and not using JavaScript.

How can I do that?

  • 写回答

1条回答 默认 最新

  • doumu8217 2012-06-13 08:00
    关注

    Using preg_match, you can achieve it.

    Assuming you are calling a url like that :

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

    You are looking for:

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

    or

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

    So you can perform:

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

    And $matches will be:

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

    edit:

    After running the example, it seems that Google return a different number when using curl, for example, on http://www.google.com, it returns:

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

    So I've updated the regex to handle the >.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题