<title>TRYPHP! Twitter API V1.1 認証ユーザーのステータス情報をメディアを付け加えて更新します。 POST statuses/update_with_media</title>
</head>
<body>

<h1>Twitter API V1.1 認証ユーザーのステータス情報をメディアを付け加えて更新します。 POST statuses/update_with_media</h1>
<!-- 説明ページurl -->
<h2><a href="http://www.tryphp.net/2014/02/20/phpapptwitterv11-update_with_media/">→説明はこちら</a></h2>
<hr/>

<?php 
//tmhOAuth.phpインクルード
require './tmhOAuth.php';
//アップロードファイル
if (!empty($_FILES)) {
    //インスタンス作成
    $tmh = new tmhOauth(array('consumer_key' => 'klE1y49JpMpxcwyK8FqL0Sl9t', 'consumer_secret' => '21FJWI4uX1ClShZfrjU2qOyq7K1fBIuXHdt1kuPw3qWCcC0AEH', 'token' => '551256276-zM2LqwlqMEwmbpZJesIiYI1Tj3V7IbuWOzY8OXc4', 'secret' => 'eIAfavhomGvSbe1yTtHFFMgm7bRRVieOkBpxNZ8pnK3Ap'));
    //アップロードファイル
    $params = array('media[]' => "@" . $_FILES['picture']['tmp_name'] . ";" . "type=" . $_FILES['picture']['type'] . ";" . "filename=" . $_FILES['picture']['name'], 'status' => $_POST['tweet']);
    $code = $tmh->user_request(array('method' => 'POST', 'url' => $tmh->url("1.1/statuses/update_with_media"), 'params' => $params, 'multipart' => true));
    //エラー処理
    if ($code != 200) {
        echo "<span style=\"color:red\">画像アップロード失敗しました。</span><br/>\n";
        $errors = $tmh->response['error'];
        var_dump($errors);
        //成功
    } else {
        echo "<span style=\"color:red\">画像アップロード成功しました!</span><br/>\n\t";
        echo "投稿<a href=\"https://twitter.com/tryphp_test\" target=\"_blank\">tryphp_test</a><br/>\n";
        //結果内容
        print_r(json_decode($tmh->response['response']));
    }
示例#2
0
 /**
  * Call a method on the Eventful API.
  *
  * @access  public
  * @param   string      arguments
  */
 function call($method, $args = array(), $type = 'rest')
 {
     /* Methods may or may not have a leading slash.  */
     $method = trim($method, '/ ');
     /* Construct the URL that corresponds to the method.  */
     $url = $this->api_root . '/' . $type . '/' . $method;
     $this->_request_uri = $url;
     // Handle the OAuth request.
     if ($this->using_oauth) {
         //create a new Oauth request.  By default this uses the HTTP AUTHORIZATION headers and HMACSHA1 signature
         if ($this->debug) {
             echo "Checking on this consumer key/secret {$this->conskey} / {$this->conssec} <br>\n";
             echo "Checking on this token key/secret {$this->oauth_token} / {$this->oauth_token_secret} <br>\n";
             echo "Using the app_key {$this->app_key} <br>\n";
         }
         $config = array('consumer_key' => $this->conskey, 'consumer_secret' => $this->conssec, 'token' => $this->oauth_token, 'secret' => $this->oauth_token_secret, 'method' => 'POST', 'use_ssl' => false, 'user_agent' => 'Eventful_PHP_API');
         $tmhOAuth = new tmhOauth($config);
         $multipart = false;
         $app_key_name = 'app_key';
         foreach ($args as $key => $value) {
             if (preg_match('/_file$/', $key)) {
                 // Check for file_upload
                 $multipart = true;
                 $app_key_name = 'oauth_app_key';
                 // Have to store the app_key in oauth_app_key so it gets sent over in the Authorization header
             }
         }
         $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url($url, ''), 'params' => array_merge(array($app_key_name => $this->{app_key}), $args), 'multipart' => $multipart));
         if ($code == 200) {
             $resp = $tmhOAuth->response['response'];
             $this->_response_data = $resp;
             if ($type === "json") {
                 $data = json_decode($resp, true);
                 if ($data[error] > 0) {
                     throw new Exception('Invalid status : ' . $data[status] . ' (' . $data[description] . ')');
                 }
             } else {
                 $data = new SimpleXMLElement($resp);
                 if ($data->getName() === 'error') {
                     $error = $data['string'] . ": " . $data->description;
                     $code = $data['string'];
                     throw new Exception($error);
                 }
             }
             return $data;
         } else {
             // Non 200 response code.
             throw new Exception('Invalid Response Code: ' . $code);
         }
     }
     // No OAuth just do a simple request
     $req = new HTTP_Request2($url);
     /* $req = new HTTP_Request2('https://api.eventful.com/rest/events/get'); */
     $req->setMethod(HTTP_Request2::METHOD_POST);
     /* Add each argument to the POST body.  */
     $req->addPostParameter('app_key', $this->app_key);
     foreach ($args as $key => $value) {
         if (preg_match('/_file$/', $key)) {
             // Treat file parameters differently.
             $req->addUpload($key, $value);
         } elseif (is_array($value)) {
             foreach ($value as $instance) {
                 $req->addPostParameter($key, $instance);
             }
         } else {
             $req->addPostParameter($key, $value);
         }
     }
     /* Send the request and handle basic HTTP errors.  */
     $response = $req->send();
     //echo " we got this status => " .  $response->getReasonPhrase() . $response->getStatus() ."\n";
     if ($response->getStatus() !== 200) {
         throw new Exception('Invalid Response Code: ' . $response->getReasonPhrase(), $response->getStatus());
     }
     /* Process the response XML through SimpleXML */
     $resp_data = $response->getBody();
     $this->_response_data = $resp_data;
     $data = new SimpleXMLElement($resp_data);
     /* Check for call-specific error messages */
     if ($data->getName() === 'error') {
         $error = $data['string'] . ": " . $data->description;
         $code = $data['string'];
         throw new Exception($error);
     }
     return $data;
 }