SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2012
    Location
    Dublin, Ireland, Ireland
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array values manipulation

    Hello,

    I have a sort of array printed from a JSON object (see at the end) and i would like my script to parse the values in [text], [sender_screen_name], [id] and [sender_id] from it. I want my program to automatically use the values from [text] and insert them into another array which then posts these values to feed a server side script for a chatbot and [sender_screen_name] and [sender_id] to be used for teh method that posts the answer to twitter.

    Code:
    $direct_message = $OAuth->get('https://api.twitter.com/1/direct_messages.json?count=1&page='); // pulls a direct message from my automated Twitter account, one at the time
    
    echo "<pre>"; // 
    
    print_r ($direct_message); // this to visualise the array in a readable format
    
    echo "</pre>";  // ditto
    
    $data = array ("say"=>"the value of [text] from the array i got from twitter should be parsed here  ", "bot_id"=>"1", "format"=>"xml"); // these are the values to be posted to the
                                                                                                                                                                                              page to feed the chatbot 
    $url = "http://localhost/program-o/gui/xml/index.php";
    
    $fields = '';
    foreach($data as $key => $value) {
      $fields .= $key . '=' . $value . '&';
    }
    rtrim($fields, '&');
    
       $post = curl_init();
    
       curl_setopt($post, CURLOPT_URL, $url);
       curl_setopt($post, CURLOPT_POST, count($data));
       curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
       curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
    
       $result = curl_exec($post);
    
       curl_close($post);
    
    
    $dm->post('direct_messages/new', array('text' =>answer from bot, 'screen_name' =>the value i am trying to get from the array i got from twitter, 'sender_id'=>wanna do the same as screen name)); // this is the method to post the answer from the chatbot back to twitter.
    Here's the array that has the data from Twitter:

    Array
    (
    [0] => stdClass Object
    (
    [sender_screen_name] => chanianaus
    [sender] => stdClass Object
    (
    [id] => 31084145
    [id_str] => 31084145
    [default_profile] => 1
    [profile_use_background_image] => 1
    [location] => Dublin
    [profile_text_color] => 333333
    [statuses_count] => 1
    [following] => 1
    [utc_offset] =>
    [profile_sidebar_border_color] => C0DEED
    [listed_count] => 0
    [name] => Swaran Singh
    [protected] =>
    [profile_background_tile] =>
    [is_translator] =>
    [profile_sidebar_fill_color] => DDEEF6
    [contributors_enabled] =>
    [profile_image_url_https] => https://si0.twimg.com/sticky/default...e_1_normal.png
    [geo_enabled] =>
    [friends_count] => 12
    [description] =>
    [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png
    [default_profile_image] => 1
    [notifications] =>
    [profile_image_url] => http://a0.twimg.com/sticky/default_p...e_1_normal.png
    [show_all_inline_media] =>
    [favourites_count] => 0
    [followers_count] => 9
    [follow_request_sent] =>
    [profile_background_color] => C0DEED
    [url] =>
    [screen_name] => chanianaus
    [verified] =>
    [lang] => en
    [created_at] => Tue Apr 14 08:22:02 +0000 2009
    [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png
    [profile_link_color] => 0084B4
    [time_zone] =>
    )

    [id_str] => 200588152493191170
    [created_at] => Thu May 10 14:08:36 +0000 2012
    [recipient_screen_name] => Infobot2012
    [recipient_id] => 428575951
    [sender_id] => 31084145
    [recipient] => stdClass Object
    (
    [id] => 428575951
    [id_str] => 428575951
    [default_profile] => 1
    [profile_use_background_image] => 1
    [location] =>
    [profile_text_color] => 333333
    [statuses_count] => 11
    [following] =>
    [utc_offset] =>
    [profile_sidebar_border_color] => C0DEED
    [listed_count] => 0
    [name] => Diego Canale
    [protected] =>
    [profile_background_tile] =>
    [is_translator] =>
    [profile_sidebar_fill_color] => DDEEF6
    [contributors_enabled] =>
    [profile_image_url_https] => https://si0.twimg.com/sticky/default_profile_images/default_profile_6_normal.png
    [geo_enabled] =>
    [friends_count] => 18
    [description] =>
    [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png
    [default_profile_image] => 1
    [notifications] =>
    [profile_image_url] => http://a0.twimg.com/sticky/default_profile_images/default_profile_6_normal.png
    [show_all_inline_media] =>
    [favourites_count] => 0
    [followers_count] => 3
    [follow_request_sent] =>
    [profile_background_color] => C0DEED
    [url] =>
    [screen_name] => Infobot2012
    [verified] =>
    [lang] => en
    [created_at] => Sun Dec 04 22:56:38 +0000 2011
    [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png
    [profile_link_color] => 0084B4
    [time_zone] =>
    )

    [id] => 2.0058815249319E+17
    [text] => test
    )

    )

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    So your references are that FOREACH items as $item in your base array, you want to put into an ARRAY $item->text, $item->sender_screen_name, $item->id, $item->recipient_id.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •