Foros del Web » Programando para Internet » PHP »

[Aporte] cURL y safe_mode = 'On' para API's (Twitter y Bit.ly)

Estas en el tema de [Aporte] cURL y safe_mode = 'On' para API's (Twitter y Bit.ly) en el foro de PHP en Foros del Web. Integrando las API's de Twitter y Bit.ly (Obtener enlaces cortos) en un servidor dedicado no tuve mayor problema, pero, en ambientes compartidos, donde normalmente PHP ...
  #1 (permalink)  
Antiguo 19/05/2010, 00:27
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
[Aporte] cURL y safe_mode = 'On' para API's (Twitter y Bit.ly)

Integrando las API's de Twitter y Bit.ly (Obtener enlaces cortos) en un servidor dedicado no tuve mayor problema, pero, en ambientes compartidos, donde normalmente PHP corre en modo seguro (safe_mode = 'On') se genera una advertencia:

Cita:
CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
Basandome en las funciones encontradas en: http://www.edmondscommerce.co.uk/cur...-or-safe-mode/ hice lo siguiente para obtener las variables que ambas clases (Twitter y Bitly) requieren: $response, $headers, $errorNumber, $errorMessage.

Código PHP:
Ver original
  1. function sm_curl($options){
  2.     // Hay que seguir redireccionamientos?
  3.     $follow = (isset($options[CURLOPT_FOLLOWLOCATION])) ? $options[CURLOPT_FOLLOWLOCATION] : false;
  4.  
  5.     // Instanciamos cURL
  6.     $go = curl_init();
  7.     $response = array();
  8.     if(ini_get('open_basedir') != '' || ini_get('safe_mode' == 'On') && $follow) {
  9.         // Aqui entramos solo si hay que seguir redireccionamientos y
  10.         // open_basedir o safe_mode estan activos
  11.  
  12.         // Eliminamos la opcion CURL para seguir redireccionamientos
  13.         unset($options[CURLOPT_FOLLOWLOCATION]);
  14.  
  15.         // Establecemos las opciones
  16.         curl_setopt_array($go, $options);
  17.  
  18.         // Ejecutamos y seguimos redireccionamientos
  19.         $response = curl_redir_exec($go);
  20.     } else {
  21.         curl_setopt_array($go, $options);
  22.         // Ejecutar
  23.         $response['response'] = curl_exec($go);
  24.         $response['headers'] = curl_getinfo($go);
  25.  
  26.         // Obtener errores
  27.         $response['errorNumber'] = curl_errno($go);
  28.         $response['errorMessage'] = curl_error($go);
  29.     }
  30.     curl_close($go);
  31.     return $response;
  32. }
  33.  
  34. // Seguir redireccionamientos con open_basedir o safe_mode = On
  35. function curl_redir_exec($ch) {
  36.     static $curl_loops = 0;
  37.     static $curl_max_loops = 20;
  38.     if ($curl_loops++>= $curl_max_loops) {
  39.         $curl_loops = 0;
  40.         return array('response' => '', 'headers' => '', 'errorNumber' => '47', 'errorMessage' => 'Too many redirects');
  41.     }
  42.     curl_setopt($ch, CURLOPT_HEADER, true);
  43.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44.     $data = curl_exec($ch);
  45.     $data = str_replace("\n\r", "\n", $data);
  46.     list($header, $data) = explode("\n\n", $data);
  47.     $headers = curl_getinfo($ch);
  48.     $http_code = $headers['http_code'];
  49.     if ($http_code == 301 || $http_code == 302) {
  50.         $matches = array();
  51.         preg_match('/Location:(.*?)\n/', $header, $matches);
  52.         $url = @parse_url(trim(array_pop($matches)));
  53.         if (!$url) {
  54.             //couldn't process the url to redirect to
  55.             $curl_loops = 0;
  56.             return array('response' => '', 'headers' => '', 'errorNumber' => 3, 'errorMessage' => 'The URL was not properly formatted (redirect).');
  57.         }
  58.         $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
  59.         if (!$url['scheme'])
  60.             $url['scheme'] = $last_url['scheme'];
  61.         if (!$url['host'])
  62.             $url['host'] = $last_url['host'];
  63.         if (!$url['path'])
  64.             $url['path'] = $last_url['path'];
  65.         $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
  66.         curl_setopt($ch, CURLOPT_URL, $new_url);
  67.         return curl_redir_exec($ch);
  68.     } else {
  69.         $curl_loops=0;
  70.         return array(
  71.             'response' => $data,
  72.             'headers' => $headers,
  73.             'errorNumber' => curl_errno($ch),
  74.             'errorMessage' => curl_error($ch),
  75.         );
  76.     }
  77. }

$options es una matriz donde se incluyen todas las opciones cURL a ejecutar, por ejemplo, en la funcion doCall() de la clase Bitly es:

Código PHP:
Ver original
  1. // set options
  2.         $options[CURLOPT_URL] = $url;
  3.         $options[CURLOPT_PORT] = self::API_PORT;
  4.         $options[CURLOPT_USERAGENT] = $this->getUserAgent();
  5.         $options[CURLOPT_FOLLOWLOCATION] = true;
  6.         $options[CURLOPT_RETURNTRANSFER] = true;
  7.         $options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();

La ejecucion original es:
Código PHP:
Ver original
  1. // init
  2.         $curl = curl_init();
  3.  
  4.         // set options
  5.         curl_setopt_array($curl, $options);
  6.  
  7.         // execute
  8.         $response = curl_exec($curl);
  9.         $headers = curl_getinfo($curl);
  10.  
  11.         // fetch errors
  12.         $errorNumber = curl_errno($curl);
  13.         $errorMessage = curl_error($curl);
  14.  
  15.         // close
  16.         curl_close($curl);

Y hay que reemplazarla por:
Código PHP:
Ver original
  1. $data = sm_curl($options);
  2.         extract($data);

Tengo un archivo common.php donde guardo las rutinas de uso mas frecuente o que son requeridas por dos o mas scripts en mis sitios, aqui coloque las funciones sm_curl() y curl_redir_exec().

Tanto para Twitter, como para Bit.ly, uso las clases creadas por Tijs Verkoyen que pueden descargar en: http://classes.verkoyen.eu/
__________________
- León, Guanajuato
- GV-Foto
  #2 (permalink)  
Antiguo 19/05/2010, 00:42
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: [Aporte] cURL y safe_mode = 'On' para API's (Twitter y Bit.ly)

Como usar esta funcion y las clases de Twitter y Bit.ly?

Ejemplo:
Código PHP:
Ver original
  1. // Ruta para buscar archivos a incluir
  2. $incdir = '/ruta/de/tu/sitio';
  3. // Matriz con opciones de configuracion, yo la cargo desde BDD
  4. $aconfig = array(
  5.     'twuser' => 'usuariotwitter',
  6.     'twpass' => 'contraseña_twitter',
  7.     'btlyuser' => 'usuariobitly',
  8.     'btlypass' => 'apiKey' // Es mejor usar api Key que la contraseña de usuario
  9. );
  10.  
  11. function twitter_update($status, $url = '') {
  12.     global $incdir, $aconfig;
  13.  
  14.     // Creamos las variables necesarias y verificamos que todo este correcto
  15.     $twuser = (isset($aconfig['twuser'])) ? $aconfig['twuser'] : '';
  16.     $twpass = (isset($aconfig['twpass'])) ? $aconfig['twpass'] : '';
  17.     $btlyuser = (isset($aconfig['btlyuser'])) ? $aconfig['btlyuser'] : '';
  18.     $btlypass = (isset($aconfig['btlypass'])) ? $aconfig['btlypass'] : '';
  19.  
  20.     if($status == '')
  21.         return 'Por favor teclea el texto de tu nuevo estado en Twitter.';
  22.  
  23.     if($twuser == '' || $twpass == '')
  24.         return 'Las opciones de Twitter no están configuradas';
  25.  
  26.     if($url != '') {
  27.         // Aqui entramos solo si es necesario recortar un enlace
  28.         if($btlyuser == '' || $btlypass == '')
  29.             return 'Las opciones de Bit.ly no están configuradas.';
  30.  
  31.         // Usamos include_once por si el proceso se ejecuta mas de una vez
  32.         include_once "$incdir/classes/bitly.php";
  33.  
  34.         // Establecemos opciones, y ejecutamos
  35.         $btly = new Bitly($btlyuser, $btlypass);
  36.         $btly->setUserAgent('Bit.ly-php');
  37.         $btres = $btly->shorten($url);
  38.  
  39.         // Verificamos si se recorto correctamente el enlace
  40.         if(isset($btres['url']) && $btres['url'] != '')
  41.             $url = ' ' . $btres['url'];
  42.         else
  43.             return 'No se pudo recortar la dirección de la página a enlazar.';
  44.        
  45.     }
  46.  
  47.     // Limpiamos un poco el texto a publicar en Twitter
  48.     $ents = array("'", '"', '<', '>', '\\');
  49.     $status = trim(str_replace($ents, '', $status));
  50.     $status = utf8_encode($status);
  51.  
  52.     // Si se incluyo un enlace, hay que garantizar que aparezca completo
  53.     if($url != '') {
  54.         $max = 140 - strlen($url);
  55.         if(strlen($status) > $max)
  56.             $status = substr($status, 0, $max);
  57.         $status .= $url;
  58.     }
  59.     include_once "$incdir/classes/twitter.php";
  60.  
  61.     // Establecemos opciones y ejecutamos
  62.     $tw = new Twitter($twuser, $twpass);
  63.     $tw->setUserAgent('Twitter-php');
  64.     $twres = $tw->updateStatus($status);
  65.  
  66.     // Verificamos si se publico el nuevo estado
  67.     if(!isset($twres['id']) || !is_numeric($twres['id']) || $twres['id'] < 0)
  68.         return 'Error desconocido: no se pudo actualizar el estado en Twitter';
  69.     return '';
  70. }
  71.  
  72.  
  73. $tw_resultado = twitter_update('Probando a actualizar el estado de Twitter desde PHP', 'http://www.forosdelweb.com/f18/aporte-curl-safe_mode-para-apis-twitter-bit-ly-808160/');
  74.  
  75. if($tw_resultado != '')
  76.     die("'Error actualizando estado en Twitter: $tw_resultado");
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 09/08/2010, 12:50
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 11 meses
Puntos: 1517
Respuesta: [Aporte] cURL y safe_mode = 'On' para API's (Twitter y Bit.ly)

No había visto este tema (¿dónde habré estado? ), agregado a mis favoritos

Edito:
Ya agregué el enlace de este aporte en el que hice
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos

Última edición por abimaelrc; 09/08/2010 a las 13:05
  #4 (permalink)  
Antiguo 09/08/2010, 13:25
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: [Aporte] cURL y safe_mode = 'On' para API's (Twitter y Bit.ly)

Se agradece.
__________________
- León, Guanajuato
- GV-Foto

Etiquetas: curl, twitter, aportes
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 3 personas




La zona horaria es GMT -6. Ahora son las 03:40.