gpt4 book ai didi

php - 使用 php 将来自 Twitter API 的数据插入 MySQL 数据库

转载 作者:可可西里 更新时间:2023-11-01 08:45:06 24 4
gpt4 key购买 nike

对 PHP 编码非常陌生 - 我已经建立了一个基于服务器的 MySQL 数据库来插入 twitter api 数据。

我可以检索 Twitter API 数据并在网络上对其进行分析 (http://www.pdanalytics.ca/StephenHarperTweets.php)。使用 PHP,如何将其插入到我的数据库中。

我搜索过,并根据一般教程进行了尝试,但仍然觉得离我认为应该是一个相当简单的任务还有很远的距离。

谢谢。

这是我的代码:

<?php

require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "xxxxxxxxxxxxxxx",
'oauth_access_token_secret' => "xxxxxxxxxxxxxxx",
'consumer_key' => "xxxxxxxxxxxxx",
'consumer_secret' => "xxxxxxxxxxxxxxxxxxx"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$requestMethod = "GET";

$getfield = '?screen_name=pmharper&count=20';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p> <em>".$string[errors][0]["message"]."</em></p>";exit();}
foreach($string as $items)
{
echo "Tweeted by: ". $items['user']['name']."<br />";
echo "Screen name: ". $items['user']['screen_name']."<br />";
echo "Tweet: ". $items['text']."<br />";
echo "Time and Date of Tweet: ".$items['created_at']."<br />";
echo "Tweet ID: ".$items['id_str']."<br />";
echo "Followers: ". $items['user']['followers_count']."<br /><hr />";
}
?>

最佳答案

这是最终代码!

我使用 MySQLi 为插入查询准备的语句。我希望您已经创建了用于插入推文的数据库和表。您需要更改以下代码的前几行并开始使用它。

<?php
define('DBHOST','localhost');
define('DBUSERNAME','databaseusername');
define('DBPASSWORD','databasepassword');
define('DBNAME','databasename');
define('TWEETTABLE','tweettablename');

require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "xxxxxxxxxxxxxxx",
'oauth_access_token_secret' => "xxxxxxxxxxxxxxx",
'consumer_key' => "xxxxxxxxxxxxx",
'consumer_secret' => "xxxxxxxxxxxxxxxxxxx"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$requestMethod = "GET";

$getfield = '?screen_name=pmharper&count=20';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p> <em>".$string[errors][0]["message"]."</em></p>";exit();}
foreach($string as $items)
{
echo "Tweeted by: ". $items['user']['name']."<br />";
echo "Screen name: ". $items['user']['screen_name']."<br />";
echo "Tweet: ". $items['text']."<br />";
echo "Time and Date of Tweet: ".$items['created_at']."<br />";
echo "Tweet ID: ".$items['id_str']."<br />";
echo "Followers: ". $items['user']['followers_count']."<br /><hr />";
echo insertTweets($items['user']['name'],$items['user']['screen_name'],$items['text'],$items['created_at'],$items['id_str'],$items['user']['followers_count']);
}

function insertTweets($name,$screen_name,$text,$created_at,$id_str,$followers_count){
$mysqli = new mysqli(DBHOST, DBUSERNAME, DBPASSWORD, DBNAME);
if ($mysqli->connect_errno) {
return 'Failed to connect to Database: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
}
$prepareStmt='INSERT INTO '.DBNAME.'.'.TWEETTABLE.' (name, screen_name, text, created_at, id_str, followers_count) VALUES (?,?,?,?,?,?);';
if ($insert_stmt = $mysqli->prepare($prepareStmt)){
$insert_stmt->bind_param('ssssid', $name,$screen_name,$text,$created_at,$id_str,$followers_count);
if (!$insert_stmt->execute()) {
$insert_stmt->close();
return 'Tweet Creation cannot be done at this moment.';
}elseif($insert_stmt->affected_rows>0){
$insert_stmt->close();
return 'Tweet Added.';
}else{
$insert_stmt->close();
return 'No Tweet were Added.';
}
}else{
return 'Prepare failed: (' . $mysqli->errno . ') ' . $mysqli->error;
}
} ?>

关于php - 使用 php 将来自 Twitter API 的数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31175316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com