/*
* 采集百度,搜狗,总次数
*/
$uid=$re['navid'];
$times=$_GET['paratertime']; $cookie_jar = tempnam('./tmp','cookie');
$ch=curl_init();
$url="http://data.dianxin.cn/apiCount.php?key=jsdfjsdkue&uid=".$uid."&date=".$times;
echo $url;
curl_setopt($ch, CURLOPT_URL ,$url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
$orders = curl_exec($ch);
curl_close($ch);
preg_match('/\{(\S*)\}/',$orders,$matches);
if(is_array($matches) && count($matches)>1)
{
list($baidusearch,$sougousearch,$adclickcount,$searchcount)=explode(",",$matches[1]);
list($name[],$result[])=explode(":",$baidusearch);
list($name[],$result[])=explode(":",$sougousearch);
list($name[],$result[])=explode(":",$adclickcount);
list($name[],$result[])=explode(":",$searchcount);
$baiducount=$result[0];
$sougoucount=$result[1];
$adclickcount=$result[2];
$searchcount=$result[3];
$count_sql="update navdetial set tatolcount=".$adclickcount.",baiducount=".$baiducount.",searchcount=".$searchcount.",sougoucount=".$sougoucount."where navids=".$ids;
echo $count_sql;
mysql_query($count_sql);
我采集来的数据放入数据库里更新,所有数据都是一样的,但是我采集的是不同的数据啊,还请大侠帮帮忙看这个循环怎么写。
* 采集百度,搜狗,总次数
*/
$uid=$re['navid'];
$times=$_GET['paratertime']; $cookie_jar = tempnam('./tmp','cookie');
$ch=curl_init();
$url="http://data.dianxin.cn/apiCount.php?key=jsdfjsdkue&uid=".$uid."&date=".$times;
echo $url;
curl_setopt($ch, CURLOPT_URL ,$url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
$orders = curl_exec($ch);
curl_close($ch);
preg_match('/\{(\S*)\}/',$orders,$matches);
if(is_array($matches) && count($matches)>1)
{
list($baidusearch,$sougousearch,$adclickcount,$searchcount)=explode(",",$matches[1]);
list($name[],$result[])=explode(":",$baidusearch);
list($name[],$result[])=explode(":",$sougousearch);
list($name[],$result[])=explode(":",$adclickcount);
list($name[],$result[])=explode(":",$searchcount);
$baiducount=$result[0];
$sougoucount=$result[1];
$adclickcount=$result[2];
$searchcount=$result[3];
$count_sql="update navdetial set tatolcount=".$adclickcount.",baiducount=".$baiducount.",searchcount=".$searchcount.",sougoucount=".$sougoucount."where navids=".$ids;
echo $count_sql;
mysql_query($count_sql);
我采集来的数据放入数据库里更新,所有数据都是一样的,但是我采集的是不同的数据啊,还请大侠帮帮忙看这个循环怎么写。
可以输出SQl语句,但是得到的都是几条一样的SQl语句。
update navdetial set tatolcount="2960",baiducount="173",searchcount="3963",sougoucount="3556"where navids=109
第二步打印preg_match('/\{(\S*)\}/',$orders,$matches);处理返回的$matches数组是否是自己想要数据
第三步看看如何遍历$matches得到想要的值。
第四步如何在遍历的循环组织sql语句
第五步打印sql是否正确
第六步存入数据库依次检查,逐条解决,方可得到正确数据。
你的问题可能大概在第二步和第三、四步
这句后面加上:$result = array();
$name = array();
另外:
有个很大的问题是:网站返回的是json格式的数据,直接json_decode($orders,true);就可以得到你的结果数组,不需要执行一大堆的explode,麻烦也且容易错误。
例如$orders的结果是:{"baiduSearch":0,"sogouSearch":0,"adClickCount":"20984","searchCount":"36235"}直接json-decode的结果是array ( 'baiduSearch' => 0, 'sogouSearch' => 0, 'adClickCount' => '23172', 'searchCount' => '38736', )
直接根据键值取就行。。
我没说取不到值,是值的覆盖问题还是追加问题。
每次循环后$name 和$result不是被覆盖了,而是加到了数组后面,那么你取$result[0],$result[1]实际上都是第一次的结果。所以让你加上初始化的代码:
$name = array();
$result = array();ps:用file_get_contents($url)是完全正常的,你为什么说不可以呢?
这样测试下看是否正确。<?php
$uid=$re['navid'];
$times=$_GET['paratertime']; $cookie_jar = tempnam('./tmp','cookie');
$ch=curl_init();
$url="http://data.dianxin.cn/apiCount.php?key=jsdfjsdkue&uid=".$uid."&date=".$times;
echo $url;
curl_setopt($ch, CURLOPT_URL ,$url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
$orders = curl_exec($ch);
curl_close($ch);
$arr = json_decode($orders,true);
$baidusearch = $arr['baiduSearch'];
$sougousearch = $arr['sogouSearch'];
$adclickcount = $arr['adClickCount'];
$searchcount = $arr['searchCount'];
$count_sql="update navdetial set tatolcount=".$adclickcount.",baiducount=".$baiducount.",searchcount=".$searchcount.",sougoucount=".$sougoucount."where navids=".$ids;
echo $count_sql;
mysql_query($count_sql);