如何在php中处理(插入db)传入的流数据?
我一直在通过guzzle库以json格式从gnip获取推特流数据 下面是我的代码如何在php中处理(插入db)传入的流数据?,php,guzzle,Php,Guzzle,我一直在通过guzzle库以json格式从gnip获取推特流数据 下面是我的代码 require_once('vendor/autoload.php'); $url =https://stream.gnip.com:443/accounts/{account_name}/publishers/twitter/streams/track/dev.json"; $username = '*****'; $password = '*****'; $headers
require_once('vendor/autoload.php');
$url =https://stream.gnip.com:443/accounts/{account_name}/publishers/twitter/streams/track/dev.json";
$username = '*****';
$password = '*****';
$headers = array('Accept-Encoding' => 'gzip');
$client = new GuzzleHttp\Client();
$client->setDefaultOption('headers/Accept-Encoding', 'gzip');
$data = $client->get($url, ['auth' => [$username, $password]]))
{
$tdata = $data->getBody();
//here i am getting json data continuously,that I have to insert in my db.
//I have written my code to insert in db and it is inserting in db but after some time it will not insert in db.
}
因此,如果有其他最佳方法或可用的php库,请向我推荐。为了获得最佳实践,您可以实现一个rest服务来在数据库中插入数据。假设你有休息服务
/data/insert
您将向该服务发送每个流数据,如
functiond insertData($data) {}
$url = 'http://domain.com/data/insert';
$fields = array(
'field1' => urlencode($data["field1"]),
'field2' => urlencode($data["field1"]),
'field3' => urlencode($data["field1"])
);
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
}
当您获得流数据时,只需运行此函数,而不必关心如何在数据库中插入数据。也许,您可以在服务端处理数据插入部分