Jump to content

Recommended Posts


So i got this pulling twitter data raw i cant for the life of me figure out how to explode items $text(tweet body

and seperate the hashtags and enter them into a database (database hase 3 fields for hashtags)

<?php
/** database cred **/
        define('DBHOST','localhost');
        define('DBUSERNAME','***');
        define('DBPASSWORD','**');
        define('DBNAME','roguebro_tweet');
        define('TWEETTABLE','twitter');
/** end database cred **/
        

        /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
        require_once('TwitterAPIExchange.php');
        $settings = array(
        'oauth_access_token' => "831981818408677377-FexWOmvCyaZYWt3TYwmodmx3gMmFIqx",
        'oauth_access_token_secret' => "L1vwbaBjsUivKn5NYVmGgve6V1lSP5THvjBk3LiadHyOj",
        'consumer_key' => "t31OianjtopHhDEdeBAjWPqj3",
        'consumer_secret' => "zFZpwrMl31BShY6CluYapaZl0K1CQPpsagBjVCMkTs2GtWHhRm"
        );
        
        /** end Twitter Credentials **/
        $url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
        $requestMethod = "GET";
        $getfield = '?screen_name=MCoCTrucos&entities=on&count=20 -rt';
        $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']);
      
$tags = $items['text']; 

$hashtags = explode("#", $tags);


 
 

        }

        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.<br><br>';
                }else{
                    $insert_stmt->close();
                    return 'No Tweet were Added.';
                }
            }else{
                return 'Prepare failed: (' . $mysqli->errno . ') ' . $mysqli->error;
            }
        }              ?>
Link to comment
https://forums.phpfreaks.com/topic/303246-php-hashtags/
Share on other sites


echo insertTweets($items['user']['name'],$items['user']['screen_name'],$items['text'],$items['created_at'],$items['id_str'],$items['user'] ['followers_count'],$hash);

/** FInd hashtags build array **/
preg_match_all("/#(\\w+)/",$items['text'],$hashtags); //now $tags is an array of hashtags


/** FInd hashtags build array **/
var_dump( $hashtags);


}

/** Insert to database **/
function insertTweets($name,$screen_name,$text,$created_at,$id_str,$followers_count,$hash){
$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, hashtags) VALUES (?,?,?,?,?,?,?)';
if ($insert_stmt = $mysqli->prepare($prepareStmt)){
$insert_stmt->bind_param('sssssid', $name,$screen_name,$text,$created_at,$id_str,$followers_count,$hash);
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.<br>';
}else{
$insert_stmt->close();
return 'No Tweet were Added.';
}
}else{
return 'Prepare failed: (' . $mysqli->errno . ') ' . $mysqli->error;
}
}

 

Link to comment
https://forums.phpfreaks.com/topic/303246-php-hashtags/#findComment-1543153
Share on other sites

so i have the array now but cant do anything database wise.

 

i have 4 hashtag fields and a link one  the system i need to use matches 

array(4) { [0]=> string(19) "#ContestOfChampions" [1]=> string(7) "#Cutoff" [2]=> string(9) "#Dormammu" [3]=> string(11) "#JaneFoster" }

is a sample array

 i would love it if i could somehow insert each field into its own mysql column to make my next steps easier

Link to comment
https://forums.phpfreaks.com/topic/303246-php-hashtags/#findComment-1543154
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.