如何在php中处理(插入数据库)传入的流数据?

时间:2014-06-06 12:32:53

标签: php guzzle

我不断通过json formate中的guzzle库从gnip获取流式传输数据。

以下是我的代码

    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库可用。

1 个答案:

答案 0 :(得分:-2)

为了获得最佳实践,您可以实现一个用于在db中插入数据的休息服务。假设您有休息服务;

/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);
}

获取流数据时,只需运行此功能,而不关心如何在db中插入数据。也许,您可以在服务端处理数据插入部分。