在laravel 5.3中集成Twitter API


error Use of undefined constant CURL_HTTP_VERSION_1_1 – assumed ‘CURL_HTTP_VERSION_1_1’

虽然我已经安装了curl

curl
cURL support => enabled
cURL Information => 7.47.0

使用thujohn/twitter

请按此步骤操作。

作曲家。文件

"require": {        
    "thujohn/twitter": "^2.2"
},

。env文件

TWITTER_CONSUMER_KEY = 
TWITTER_CONSUMER_SECRET = 
TWITTER_ACCESS_TOKEN = 
TWITTER_ACCESS_TOKEN_SECRET =

路由文件

Route::get('twitterUserTimeLine', 'TwitterController@twitterUserTimeLine');
Route::post('tweet', ['as'=>'post.tweet','uses'=>'TwitterController@tweet']);

TwitterController文件

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Twitter;
use File;
class TwitterController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function twitterUserTimeLine()
    {
        $data = Twitter::getUserTimeline(['count' => 6, 'format' => 'array']);
        return view('twitter',compact('data'));
    }
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function tweet(Request $request)
    {
        $this->validate($request, [
            'tweet' => 'required'
        ]);
        $newTwitte = ['status' => $request->tweet];

        if(!empty($request->images)){
            foreach ($request->images as $key => $value) {
                $uploaded_media = Twitter::uploadMedia(['media' => File::get($value->getRealPath())]);
                if(!empty($uploaded_media)) {
                    $newTwitte['media_ids'][$uploaded_media->media_id_string] = $uploaded_media->media_id_string;
                }
            }
        }
        $twitter = Twitter::postTweet($newTwitte);

        return back();
    }
}

查看文件

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 5 - Twitter API</title>
    <link rel="stylesheet" type="text/css"    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h2>Laravel 5 - Twitter API</h2>
    <form method="POST" action="{{ route('post.tweet') }}" enctype="multipart/form-data">
        {{ csrf_field() }}
        @if(count($errors))
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.
                <br/>
                <ul>
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
        <div class="form-group">
            <label>Add Tweet Text:</label>
            <textarea class="form-control" name="tweet"></textarea>
        </div>
        <div class="form-group">
            <label>Add Multiple Images:</label>
            <input type="file" name="images[]" multiple class="form-control">
        </div>
        <div class="form-group">
            <button class="btn btn-success">Add New Tweet</button>
        </div>
    </form>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th width="50px">No</th>
                <th>Twitter Id</th>
                <th>Message</th>
                <th>Images</th>
                <th>Favorite</th>
                <th>Retweet</th>
            </tr>
        </thead>
        <tbody>
            @if(!empty($data))
                @foreach($data as $key => $value)
                    <tr>
                        <td>{{ ++$key }}</td>
                        <td>{{ $value['id'] }}</td>
                        <td>{{ $value['text'] }}</td>
                        <td>
                            @if(!empty($value['extended_entities']['media']))
                                @foreach($value['extended_entities']['media'] as $v)
                                    <img src="{{ $v['media_url_https'] }}" style="width:100px;">
                                @endforeach
                            @endif
                        </td>
                        <td>{{ $value['favorite_count'] }}</td>
                        <td>{{ $value['retweet_count'] }}</td>
                    </tr>
                @endforeach
            @else
                <tr>
                    <td colspan="6">There are no data.</td>
                </tr>
            @endif
        </tbody>
    </table>
</div>
</body>
</html>

显示视频

https://www.youtube.com/watch?v=gci8EklvVKU

最新更新