【问题标题】:twitter api integration in laravel 5.3laravel 5.3 中的 twitter api 集成
【发布时间】:2016-11-06 14:43:08
【问题描述】:
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

【问题讨论】:

    标签: php laravel curl twitter


    【解决方案1】:

    请按此步骤操作。

    composer.json 文件

    "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 App\Http\Controllers;
    
    use Illuminate\Http\Request;
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-18
      • 2013-11-18
      • 2017-01-26
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      相关资源
      最近更新 更多