1

Есть api twitter c с которым я работаю через скрипт:

var arrtweets = [];
var x;
//Function url formater
function url(searchTerm, numOfTweets){
  result = 'http://catcoin.ru/twitter.php?q=' + searchTerm + '&count='+ numOfTweets;
  return result;
}

// API Sendler
var get100Tweet = function(data) {
  $.each(data.statuses, function(index, item) {
    arrtweets.push(item.text);
  });
x = Math.floor((Math.random() * 100) + 1);
y = arrtweets[x].toString();
$('#list').html(y);
};

function TweetRandom(name) {
    document.getElementById("list").innerHTML = $.get(url(name,100),get100Tweet);
}

Мне необходимо вместо поисковой выдачи получить последние твиты пользователя, которые позволяет api

<?php

  $http_origin = $_SERVER['HTTP_ORIGIN'];

  if ( strrpos($http_origin, "catcoin.ru") || strrpos($http_origin, "jsfiddle.net") ){
      header("Access-Control-Allow-Origin: $http_origin");
  }

  header('Content-Type: application/json');

  ini_set('display_errors', 1);
  require_once('TwitterAPIExchange.php');

  /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
  $settings = array(
      'oauth_access_token' => "",
      'oauth_access_token_secret' => "",
      'consumer_key' => "",
      'consumer_secret' => ""
  );


  /** Perform a GET request and echo the response **/
  /** Note: Set the GET field BEFORE calling buildOauth(); **/

  $url = 'https://api.twitter.com/1.1/search/tweets.json';
  $getfield = '?'.$_SERVER['QUERY_STRING'];
  $requestMethod = 'GET';
  $twitter = new TwitterAPIExchange($settings);

  $api_response = $twitter ->setGetfield($getfield)
                       ->buildOauth($url, $requestMethod)
                       ->performRequest();

  echo $api_response;

https://gist.github.com/anonymous/234284d3eb6817969d9e04e497f5ad4a

4
  • а что в twitter.php?
    – L. Vadim
    16 янв 2017 в 9:14
  • К сожалению пока не могу добавить код php, добавлю ближе к вечеру. 16 янв 2017 в 10:22
  • Напишите сюда когда добавите, спасибо
    – L. Vadim
    16 янв 2017 в 10:24
  • Дополнил php файлами 28 янв 2017 в 19:46

1 ответ 1

2

К сожалению не нашел, как это сделать на чистом php, однако нашел библиотеку, которая получает эти данные.

https://github.com/thujohn/twitter/

Так же можно подключить через composer:

composer require thujohn/twitter

В моём случае вызов такой:

   <?php 
      echo Twitter::getUserTimeline(['screen_name' => 'vdvwolf',
      'count' => 200, 'format' => 'json']);

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.