Twitter API Post Image

Discussion in 'PHP' started by scottlpool2003, Oct 7, 2013.

  1. #1
    I'm really not enjoying the documentation for the Twitter API...

    There doesn't appear to be too much support for it either.

    I've managed to get it working posting simple status updates, but can't seem to get posting images to work.

    Here is what I have thus far:

    <?php
    require 'tmhOAuth.php';
    require 'tmhUtilities.php';
    $tmhOAuth = new tmhOAuth(array(
      'consumer_key'    => '',
      'consumer_secret' => '',
      'user_token'      => '',
      'user_secret'    => '',
    ));
    
    $image = 'http://localhost/api/twitter/oAuth/test.jpg';
    
    $code = $tmhOAuth->request(
      'post',
      'https://api.twitter.com/1.1/statuses/update_with_media.json',
      array(
        'media[]'  => "@{$image};type=image/jpeg;filename={$image}",
        'status'  => 'Picture time'
      ),
      true,
      true
    );
    
    //var_dump($tmhOAuth->response);
    
    if ($code == 200) {
      tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
    } else {
      tmhUtilities::pr($tmhOAuth->response['response']);
    }
    
    ?>
    PHP:
    All I seem to get is
    Reading up on what this means but seem to be getting mixed messages everything from incorrect tokens to 401?

    Any advise?
     
    scottlpool2003, Oct 7, 2013 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    Got this working with the following amendment:

    <?php
    require 'tmhOAuth.php';
    require 'tmhUtilities.php';
    $tmhOAuth = new tmhOAuth(array(
      'consumer_key'    => '',
      'consumer_secret' => '',
      'user_token'      => '',
      'user_secret'    => '',
      'curl_ssl_verifypeer'  => false
    
    ));
    
    
    $image_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'test.jpg';
    $name  = basename($image_path);
    
    $code = $tmhOAuth->request(
        'POST',
        $tmhOAuth->url('1.1/statuses/update_with_media'),
        array(
            'media[]'  => "@{$image_path};type=image/jpeg;filename={$name}",
            'status'  => 'Picture time',
          ),
          true, // use auth
          true  // multipart
    );
    
    
    
    if ($code == 200) {
      tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
    } else {
      tmhUtilities::pr($tmhOAuth->response['response']);
    }
    
    ?>
    PHP:
     
    scottlpool2003, Oct 7, 2013 IP
  3. realturk

    realturk Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #3
    thanx :) i got it... from url upload ??? are you know ?
     
    Last edited: Oct 9, 2013
    realturk, Oct 9, 2013 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    Sorry I don't understand? The above script is just the Twitter post part of a news article with image upload from form...
     
    scottlpool2003, Oct 10, 2013 IP
  5. realturk

    realturk Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #5
    ok no problem, i do it. thanx :)
     
    realturk, Oct 10, 2013 IP