2013-12-17 2 views
0

저는 PHP와 cURL을 통해 이미지가 포함 된 메시지를 Twitter에 게시하려고합니다. 메시지를 위해 작동하지만 이미지를 추가하는 방법을 모르겠습니다. documentation 가입일PHP로 트위터에 이미지를 게시하는 방법은 무엇입니까?

: POST 상태/업데이트 달리

원시 다중 데이터를 기대 이 방법. 귀하의 POST 요청의 콘텐츠 유형과 형태의 데이터를으로 multipart /에 설정해야합니다 이것은 PHP입니다

미디어 [] 매개 변수 :

<?php 
class Tweet { 
public $url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'; 

function the_nonce(){ 
    $nonce = base64_encode(uniqid()); 
    $nonce = preg_replace('~[\W]~','',$nonce); 
    return $nonce; 
} 

function get_DST($status){ 

    $url = $this->url; 

    $consumer_key = "[removed]"; 
    $nonce = $this->the_nonce(); 
    $sig_method = 'HMAC-SHA1'; 
    $timestamp = time(); 
    $version = "1.0"; 
    $token = "[removed]"; 
    $access_secret = "[removed]"; 
    $consumer_secret = "[removed]"; 

    $param_string = 'oauth_consumer_key='.$consumer_key. 
      '&oauth_nonce='.$nonce. 
      '&oauth_signature_method='.$sig_method. 
      '&oauth_timestamp='.$timestamp. 
      '&oauth_token='.$token. 
      '&oauth_version='.$version. 
      '&status='.rawurlencode($status); 
    $sig_base_string = 'POST&'.rawurlencode($url).'&'.rawurlencode($param_string); 
    $sig_key = rawurlencode($consumer_secret).'&'.rawurlencode($access_secret); 

    $tweet_sig = base64_encode(hash_hmac('sha1', $sig_base_string, $sig_key, true)); 

    $DST = 'OAuth oauth_consumer_key="'.rawurlencode($consumer_key).'",'. 
     'oauth_nonce="'.rawurlencode($nonce).'",'. 
     'oauth_signature="'.rawurlencode($tweet_sig).'",'. 
     'oauth_signature_method="HMAC-SHA1",'. 
     'oauth_timestamp="'.rawurlencode($timestamp).'",'. 
     'oauth_token="'.rawurlencode($token).'",'. 
     'oauth_version="1.0"'; 
    return $DST; 
} 

function set($status){ 
$url = $this->url; 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->get_DST($status))); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status)); 
curl_setopt($ch, CURLOPT_URL, $url); 
$result = json_decode(curl_exec($ch)); 
$resultString = print_r($result,true);; 
curl_close($ch); 
} 

$status->set("hello"); 
?> 

미디어 [] 부분을 추가하는 방법 그걸로?

+2

내가 [트위터의 OAuth는 (https://dev.twitter.com/docs/auth/oauth/faq) 난 내 서버에 이미지를 업로드하여이 (컬없이)하고있어, 수 이미지의 경로, 페이지 링크를 추가하고 함수로 보내면 해당 유형의 솔루션이 작동합니까? 그렇다면, 나는 당신을 돕기 위해 일부 코드를 게시 할 것입니다. – DannyG

+0

@DannyG 솔루션으로 갈 것입니다. 그것은 우아하지는 않지만 결과는 동일하며 구현하기가 더 쉽습니다. – philshem

+0

@DannyG 그 덕분에, 고마워! 나는 당신의 코드를 기대하고있다. – Jay

답변

0

확실하지 Matt Harris '라이브러리가 당신을 위해 할 것입니다,하지만 코드가 난 당신이 이미지 트윗을 게시 할 수있는 스크립트를 만든

require_once ('your_app_tokens.php'); 
require_once ('tmhOAuth.php'); 

$connection = new tmhOAuth(array(
    'consumer_key' => $consumer_key, 
    'consumer_secret' => $consumer_secret, 
    'user_token'  => $user_token, //stored in session or whatever else 
    'user_secret'  => $User_secret 
)); 
$message ="test"; 
    $image = "image.jpg"; // must be in the same directory 
    $extension = "jpg"; 
     $post = $connection->request(
      'POST', 
     'https://api.twitter.com/1.1/statuses/update_with_media.json', 
      array(
      'media[]' => "@{$image};type=image/{$extension};filename={$image}", 
      'status' => $message 
     ), 


    true, 
       true 
      ); 
      $code = $connection->response; 

//if the code is 200 it's all fine 
0

오히려 간단합니다. 난 당신의 서버에 이미지를 업로드하는 URL을 사용하여 PHP 글꼴을 사용하고 있습니다. 그 후, 그것은 서버에서 트위터로 업로드됩니다. 사진이있는 트윗을 보내는 데 사용할 수있는 이미지 ID가 있습니다.

내 github projet입니다. 모든 것이 내부에 있으며, 사용하기 쉽고 설치하기 쉽습니다.

https://github.com/florianchapon/twitter-poster

관련 문제