Mkportal Hispano
07 de 09 del 2010, 01:52:50 *
Bienvenido(a), Visitante. Por favor, ingresa o regístrate.
¿Perdiste tu email de activación?

Ingresar con nombre de usuario, contraseña y duración de la sesión
 
   Inicio   Ayuda Ingresar Registrarse  
Páginas: [1]   Ir Abajo
  Imprimir  
Autor Tema: Sincronizar Mkportal con Twitter y acortando las direcciones con Bit.ly  (Leído 348 veces)
0 Usuarios y 1 Visitante están viendo este tema.
javiotero
Cogiéndole el gustillo al foro
**
Desconectado Desconectado

Sexo: Masculino
Mensajes: 78

AGRADECIMIENTOS
-Agradecido: 12
-
Recibido: 9


Esto es una fiesta


WWW
« : 09 de 03 del 2010, 00:23:58 »

Hola he conseguido sincronizar la web de http://www.gsbrownsea.es con su twitter http://twitter.com/gsbrownsea, añadiendo dos librerías a la carpeta modules/news y unas cuantas lineas de código a la función red_data en modules/news/index.php

Os interesa saber como hacerlo??

Mañana o pasado mañana publico la solución....

Nos vemos!!

ACTUALIZO

En el servidor PHP teneis que tener habilitados
  • cUrl
  • XML

Lo primero de todo os hacen falta dos librerias.... "Bitly.class.php" y "twitter.lib.php". Las podeis encontrar en internet, pero tambien os las pego al final de este post  tongue2

Os vais al archivo mkportal/modules/news/index.php y sobre la linea 662, despues de los IF's.... dentro de la funcion reg_data() añadis:

Despues de....
Código:
if ($approval == "3" && !$mkportals->member['g_access_cp']) {
$mklib->message_page ($mklib->lang['notify_adv']);
exit;
}

añadis....

Código:
// INICIO para que se actualice la cuenta de Twitter

//obtenemos datos de la BBDD
$query = $DB->query("SELECT titolo,id from mkp_news where id=(select max(id) from mkp_news where validate = 1)");
$row = $DB->fetch_row($query);
echo $row['id'];
$urlMkportal="http://www.gsbrownsea.es/index.php?ind=news&op=news_show_single&ide=".$row['id'];

require("bitly.class.php");

// Creating an instance of the bitly-class
$bitly = new Bitly();

// Setting the authentication for your Bit.ly user account. http://bit.ly/account/your_api_key
$login = "username";
$apiKey = "api-key";
$bitly->setAuthentication($login, $apiKey);

// Getting a short URL by Bit.ly
$urlAcortada = $bitly->getShortURL($urlMkportal);

// Expand a short Bit.ly URL to the original URL
//echo $bitly->getLongURL("http://bit.ly/7px4Q2");
// result: http://www.google.com/

//libreria  de twiter
require "twitter.lib.php";

//configuracion de twitter
$username = "nombreUsuario";
$password = "Contraseña";

//creamos el objetos de twitter
$twitter = new Twitter($username, $password);

//actualizo mi status
$status = $twitter->updateStatus($row['titolo']." Mas info en ".$urlAcortada);

// FIN para que se actualice la cuenta de Twitter

y la funcion terminaria con....
Código:
$DB->close_db();
Header("Location: index.php?ind=news&op=news_show_category&idc=$categoria");
exit;


LIBRERIA PARA UTILIZAR BITLY "Bitly.class.php"
Código:
<?php
/**
 * File:    Bitly.class.php
 * Author:  Dennis Madsen <dennis@demaweb.dk>
 * Created: 03/01/2010
 * Updated  04/01/2010
 * Version: 1.0
*/
class bitly {
private $login "";
private $apiKey "";
private $url "http://api.bit.ly/";
private $version "2.0.1";

public $history true;

/*
* Creating a short Bit.ly URL from a long URL.
*/
public function getShortURL($longUrl) {
// Creating the parameters for the request
$additionalParameterList = array(
"longUrl" => urlencode($longUrl)
);
$parameters $this->getParameters($additionalParameterList);

// Sending the request to Bit.ly
$response $this->sendRequest("shorten"$parameters);

// Parsing the response and return the short url
$json = @json_decode($response,true);
return $json['results'][$longUrl]['shortUrl'];
}

/*
* Returning the original URL from a short Bit.ly URL.
*/
public function getLongURL($shortUrl) {
// Find the hash from the short URL
$hash $this->getHash($shortUrl);

// Creating the parameters for the request
$additionalParameterList = array(
"shortUrl" => urlencode($shortUrl)
);
$parameters $this->getParameters($additionalParameterList);

// Sending the request to Bit.ly
$response $this->sendRequest("expand"$parameters);

// Parsing the response and return the long url
$json = @json_decode($response,true);
return $json['results'][$hash]['longUrl'];
}

/*
* Set your Bit.ly authentication.
* http://bit.ly/account/your_api_key
*/

public function setAuthentication($login$apiKey) {
$this->login $login;
$this->apiKey $apiKey;
}

private function getParameters($additionalParameterList=null) {
$parameters="";

foreach ($additionalParameterList as $key => $value) {
if (!empty($parameters)) $parameters .= "&";
$parameters .= $key."=".$value;
}

if (!empty($parameters)) $parameters .= "&";
$parameters .= "login=".$this->login."&apiKey=".$this->apiKey;
if (!empty($this->version)) $parameters .= "&version=".$this->version;
if ($this->history$parameters .= "&history=".$this->history;

return $parameters;
}

private function sendRequest($method$parameters) {
$url $this->url $method "?" $parameters;

// Using cURL to handle the request
// cURL Extension must be enabled in your PHP environment
// Further information: http://php.net/manual/en/book.curl.php
$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$response curl_exec($ch);
curl_close($ch);

// Validating the response
$this->validateResponse($response);

return $response;
}

private function validateResponse($response) {
// Checking the statusCode
$json = @json_decode($response,true);
$errorCode $json['errorCode'];
$errorMessage $json['errorMessage'];

// Printing error message if error exists
if ($errorCode!=0)
die("Bit.ly error: ".$errorMessage.". Error code: ".$errorCode.".");
}

private function getHash($bitlyUrl) {
$searchString "bit.ly/";
$pos strpos($bitlyUrl$searchString);
if ($pos === false)
die("Invalid Bit.ly short url.");
return substr($bitlyUrl, ($pos+strlen($searchString)));
}
}
?>
« Última modificación: 09 de 03 del 2010, 21:37:50 por javiotero » En línea

Información de Soporte:
Portal: MK Portal v.C1.2.2
Foro: SMF v.1.1.9
javiotero
Cogiéndole el gustillo al foro
**
Desconectado Desconectado

Sexo: Masculino
Mensajes: 78

AGRADECIMIENTOS
-Agradecido: 12
-
Recibido: 9


Esto es una fiesta


WWW
« Respuesta #1 : 09 de 03 del 2010, 21:34:56 »

LIBRERIA PARA UTILIZAR TWITTER "twitter.lib.php"
Código:
<?php
/*
 * Copyright (c) <2008> Justin Poliey <jdp34@njit.edu>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Twitterlibphp is a PHP implementation of the Twitter API, allowing you
 * to take advantage of it from within your PHP applications.
 *
 * @author Justin Poliey <jdp34@njit.edu>
 * @package twitterlibphp
 */

/**
 * Twitter API abstract class
 * @package twitterlibphp
 */
abstract class TwitterBase {

/**
 * the last HTTP status code returned
 * @access private
 * @var integer
 */
private $http_status;

/**
 * the whole URL of the last API call
 * @access private
 * @var string
 */
private $last_api_call;

/**
 * the application calling the API
 * @access private
 * @var string
 */
private $application_source;

/**
 * Returns the 20 most recent statuses from non-protected users who have set a custom user icon.
 * @param string $format Return format
 * @return string
 */
function getPublicTimeline($options = array(), $format 'xml') {
return $this->apiCall('statuses/public_timeline''get'$format$options);

}

/**
 * Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getFriendsTimeline($options = array(), $format 'xml') {
return $this->apiCall('statuses/friends_timeline''get'$format$options);
}

/**
 * Returns the 20 most recent statuses posted from the authenticating user.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getUserTimeline($options = array(), $format 'xml') {
return $this->apiCall('statuses/user_timeline''get'$format$optionstrue);
}

  
/**
   * Returns the 20 most recent mentions (status containing @username) for the authenticating user.
   * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
  
function getMentions($options = array(), $format 'xml') {
    return 
$this->apiCall("statuses/mentions"'get'$format$options);
  }

  
/**
 * Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
   * @deprecated
 */
function getReplies($options = array(), $format 'xml') {
return $this->apiCall('statuses/replies''get'$format$options);
}

/**
 * Returns a single status, specified by the $id parameter.
 * @param string|integer $id The numerical ID of the status to retrieve
 * @param string $format Return format
 * @return string
 */
function getStatus($id$format 'xml') {
return $this->apiCall("statuses/show/{$id}"'get'$format, array(), false);
}

/**
 * Updates the authenticated user's status.
 * @param string $status Text of the status, no URL encoding necessary
 * @param string|integer $reply_to ID of the status to reply to. Optional
 * @param string $format Return format
 * @return string
 */
function updateStatus($status$reply_to null$format 'xml') {
$options = array('status' => $status);
if ($reply_to) {
$options['in_reply_to_status_id'] = $reply_to;
}
    return 
$this->apiCall('statuses/update''post'$format$options);
}

/**
 * Destroys the status specified by the required ID parameter. The authenticating user must be the author of the specified status.
 * @param integer|string $id ID of the status to destroy
 * @param string $format Return format
 * @return string
 */
function destroyStatus($id$format 'xml') {
    return 
$this->apiCall("statuses/destroy/{id}"'post'$format$options);
}

/**
 * Returns the authenticating user's friends, each with current status inline.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getFriends($options = array(), $format 'xml') {
return $this->apiCall('statuses/friends''get'$format$optionsfalse);
}

/**
 * Returns the authenticating user's followers, each with current status inline.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getFollowers($options = array(), $format 'xml') {
return $this->apiCall('statuses/followers''get'$format$options);
}

/**
 * Returns extended information of a given user.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function showUser($options = array(), $format 'xml') {
if (!array_key_exists('id'$options) && !array_key_exists('user_id'$options) && !array_key_exists('screen_name'$options)) {
$options['id'] = substr($this->credentials0strpos($this->credentials':'));
}
return $this->apiCall('users/show''get'$format$optionsfalse);
}

/**
 * Returns a list of the 20 most recent direct messages sent to the authenticating user.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getMessages($options = array(), $format 'xml') {
return $this->apiCall('direct_messages''get'$format$options);
}

/**
 * Returns a list of the 20 most recent direct messages sent by the authenticating user.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getSentMessages($options = array(), $format 'xml') {
return $this->apiCall('direct_messages/sent''get'$format$options);
}

/**
 * Sends a new direct message to the specified user from the authenticating user.
 * @param string $user The ID or screen name of a recipient
 * @param string $text The message to send
 * @param string $format Return format
 * @return string
 */
function newMessage($user$text$format 'xml') {
$options = array(
'user' => $user,
'text' => $text
);
return $this->apiCall('direct_messages/new''post'$format$options);
}

/**
 * Destroys the direct message specified in the required $id parameter.
 * @param integer|string $id The ID of the direct message to destroy
 * @param string $format Return format
 * @return string
 */
function destroyMessage($id$format 'xml') {
return $this->apiCall("direct_messages/destroy/{$id}"'post'$format$options);
}

/**
 * Befriends the user specified in the ID parameter as the authenticating user.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function createFriendship($options = array(), $format 'xml') {
if (!array_key_exists('follow'$options)) {
      
$options['follow'] = 'true';
    }
return $this->apiCall('friendships/create''post'$format$options);
}

/**
 * Discontinues friendship with the user specified in the ID parameter as the authenticating user.
 * @param integer|string $id The ID or screen name of the user to unfriend
 * @param string $format Return format
 * @return string
 */
function destroyFriendship($id$format 'xml') {
$options = array('id' => $id);
return $this->apiCall('friendships/destroy''post'$format$options);
}

/**
 * Tests if a friendship exists between two users.
 * @param integer|string $user_a The ID or screen name of the first user
 * @param integer|string $user_b The ID or screen name of the second user
 * @param string $format Return format
 * @return string
 */
function friendshipExists($user_a$user_b$format 'xml') {
$options = array(
'user_a' => $user_a,
'user_b' => $user_b
);
return $this->apiCall('friendships/exists''get'$format$options);
}

/**
 * Returns an array of numeric IDs for every user the specified user is followed by.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getFriendIDs($options = array(), $format 'xml') {
return $this->apiCall('friends/ids''get'$format$options);
}

/**
 * Returns an array of numeric IDs for every user the specified user is following.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getFollowerIDs($options = array(), $format 'xml') {
return $this->apiCall('followers/ids''get'$format$options);
}

/**
 * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; returns a 401 status code and an error message if not.
 * @param string $format Return format
 * @return string
 */
function verifyCredentials($format 'xml') {
return $this->apiCall('account/verify_credentials''get'$format, array());
}

  
/**
 * Returns the remaining number of API requests available to the requesting user before the API limit is reached for the current hour.
 * @param boolean $authenticate Authenticate before calling method
   * @param string $format Return format
 * @return string
 */
function rateLimitStatus($authenticate false$format 'xml') {
return $this->apiCall('account/rate_limit_status''get'$format, array(), $authenticate);
}

/**
 * Ends the session of the authenticating user, returning a null cookie.
 * @param string $format Return format
 * @return string
 */
function endSession($format 'xml') {
return $this->apiCall('account/end_session''post'$format, array());
}

/**
 * Sets which device Twitter delivers updates to for the authenticating user.
 * @param string $device The delivery device used. Must be sms, im, or none
 * @return string
 */
function updateDeliveryDevice($device$format 'xml') {
$options = array('device' => $device);
return $this->apiCall('account/update_delivery_device''post'$format$options);
}

/**
 * Sets one or more hex values that control the color scheme of the authenticating user's profile page on twitter.com.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function updateProfileColors($options$format 'xml') {
return $this->apiCall('account/update_profile_colors''post'$format$options);
}

/**
 * Sets values that users are able to set under the "Account" tab of their settings page.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function updateProfile($options$format 'xml') {
return $this->apiCall('account/update_profile''post'$format, array());
}


/**
 * Returns the 20 most recent favorite statuses for the authenticating user or user specified by the ID parameter in the requested format.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getFavorites($options = array(), $format 'xml') {
return $this->apiCall('favorites''get'$format$options);
}

/**
 * Favorites the status specified in the ID parameter as the authenticating user.
 * @param integer|string $id The ID of the status to favorite
 * @param string $format Return format
 * @return string
 */
function createFavorite($id$format 'xml') {
$options = array('id' => $id);
return $this->apiCall('favorites/create''post'$format$options);
}

/**
 * Un-favorites the status specified in the ID parameter as the authenticating user.
 * @param integer|string $id The ID of the status to un-favorite
 * @param string $format Return format
 * @return string
 */
function destroyFavorite($id$format 'xml') {
$options = array('id' => $id);
return $this->apiCall('favorites/destroy''post'$format$options);
}

/**
 * Enables notifications for updates from the specified user to the authenticating user.
 * @param integer|string $id The ID or screen name of the user to follow
 * @param string $format Return format
 * @return string
 */
function follow($id$format 'xml') {
$options = array('id' => $id);
return $this->apiCall('notifications/follow''post'$format$options);
}

/**
 * Disables notifications for updates from the specified user to the authenticating user.
 * @param integer|string $id The ID or screen name of the user to leave
 * @param string $format Return format
 * @return string
 */
function leave($id$format 'xml') {
$options = array('id' => $id);
return $this->apiCall('notifications/leave''post'$format$options);
}

/**
 * Blocks the user specified in the ID parameter as the authenticating user.
 * @param integer|string $id The ID or screen name of the user to block
 * @param string $format Return format
 * @return string
 */
function createBlock($id$format 'xml') {
$options = array('id' => $id);
return $this->apiCall('blocks/create''post'$format$options);
}

/**
 * Unblocks the user specified in the ID parameter as the authenticating user.
 * @param integer|string $id The ID or screen name of the user to unblock
 * @param string $format Return format
 * @return string
 */
function destroyBlock($id$format 'xml') {
$options = array('id' => $id);
return $this->apiCall('blocks/destroy''post'$format$options);
}

  
/**
 * Returns if the authenticating user is blocking a target user.
 * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function blockExists($options$format 'xml') {
return $this->apiCall('blocks/exists''get'$format$options);
}

  
/**
 * Returns an array of user objects that the authenticating user is blocking.
   * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getBlocking($options$format 'xml') {
return $this->apiCall('blocks/blocking''get'$format$options);
}

  
/**
 * Returns an array of numeric user ids the authenticating user is blocking.
   * @param array $options Options to pass to the method
 * @param string $format Return format
 * @return string
 */
function getBlockingIDs($format 'xml') {
return $this->apiCall('blocks/blocking/ids''get'$format, array());
}

/**
 * Returns the string "ok" in the requested format with a 200 OK HTTP status code.
 * @param string $format Return format
 * @return string
 */
function test($format 'xml') {
return $this->apiCall('help/test''get'$format, array(), false);
}

/**
 * Returns the last HTTP status code
 * @return integer
 */
function lastStatusCode() {
return $this->http_status;
}

/**
 * Returns the URL of the last API call
 * @return string
 */
function lastApiCall() {
return $this->last_api_call;
}
}

/**
 * Access to the Twitter API through HTTP auth
 * @package twitterlibphp
 */
class Twitter extends TwitterBase {

/**
 * the Twitter credentials in HTTP format, username:password
 * @access private
 * @var string
 */
var $credentials;

/**
 * Fills in the credentials {@link $credentials} and the application source {@link $application_source}.
 * @param string $username Twitter username
 * @param string $password Twitter password
 * @param $source string Optional. Name of the application using the API
 */
function __construct($username$password$source null) {
$this->credentials sprintf("%s:%s"$username$password);
$this->application_source $source;
}

/**
 * Executes an API call
 * @param string $twitter_method The Twitter method to call
   * @param string $http_method The HTTP method to use
   * @param string $format Return format
   * @param array $options Options to pass to the Twitter method
 * @param boolean $require_credentials Whether or not credentials are required
 * @return string
 */
protected function apiCall($twitter_method$http_method$format$options$require_credentials true) {
$curl_handle curl_init();
    
$api_url sprintf('http://twitter.com/%s.%s'$twitter_method$format);
    if ((
$http_method == 'get') && (count($options) > 0)) {
      
$api_url .= '?' http_build_query($options);
    }
curl_setopt($curl_handleCURLOPT_URL$api_url);
if ($require_credentials) {
curl_setopt($curl_handleCURLOPT_USERPWD$this->credentials);
}
if ($http_method == 'post') {
curl_setopt($curl_handleCURLOPT_POSTtrue);
      
curl_setopt($curl_handleCURLOPT_POSTFIELDShttp_build_query($options));
}
curl_setopt($curl_handleCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($curl_handleCURLOPT_HTTPHEADER, array('Expect:'));
$twitter_data curl_exec($curl_handle);
$this->http_status curl_getinfo($curl_handleCURLINFO_HTTP_CODE);
$this->last_api_call $api_url;
curl_close($curl_handle);
return $twitter_data;
}

}

/**
 * TODO: Add TwitterOAuth class
 */
?>

Los siguientes miembros han considerado útil este post:

Nosferatus

1 Miembro
En línea

Información de Soporte:
Portal: MK Portal v.C1.2.2
Foro: SMF v.1.1.9
Nosferatus
Saludos
Moderador
*
Desconectado Desconectado

Sexo: Masculino
Mensajes: 768

AGRADECIMIENTOS
-Agradecido: 126
-
Recibido: 154



WWW
« Respuesta #2 : 10 de 03 del 2010, 15:07:02 »

B ueno, no uso Twitter pero es muy interesante  good
En línea

Información de Soporte:
Portal: MK Portal v.C1.2.2
Foro: SMF v.SMF 1.1.10
Páginas: [1]   Ir Arriba
  Imprimir  
 
Ir a:  

Impulsado por MySQL Impulsado por PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC XHTML 1.0 válido! CSS válido!
Página creada en 0.221 segundos con 22 consultas.
 

MKPortal C1.2.2 ©2003-2009 mkportal.it
Fundador mkportal.es: Cur aka J.Corbillon ©2005-2010

Página generada en 0.12111 segundos con 14 consultas a la base de datos