Could you explain to me how I get news from Facebook in my application?

In my application. I get a user channel and a user news ticker. At first I have no questions, but with the second I have problems. How can I access the ticker using php?

+4
source share
2 answers

In my experience, a ticker is just an abridged version of news for users using the β€œstory”

Here is an exemplary "with only one request" batch request, which I use to display ticker information from user news feeds.

user / home https://developers.facebook.com/docs/reference/api/user/#home

user list filtering results https://developers.facebook.com/docs/reference/fql/stream_filter/

API request:

<?php $Ticker = $facebook->api('/me/home?fields=id,story%26'.$access_token.''); echo '<pre>'; print_r($Ticker); echo '</pre>'; ?> 

Package API request:

 <?php $Ticker = '/me/home?fields=id,story%26'.$access_token.''; $queries = array( array('method' => 'GET', 'relative_url' => ''.$Ticker.'') ); $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST'); $MEticker = json_decode($batchResponse[0]['body'], true); echo '<pre>'; print_r($MEticker); echo '</pre>'; ?> 

+2
source

Many thanks! I almost got it! :)

Another way to get a ticker:

 <?php $res = $app->facebook->get_friends_news('me',$access_token); print_r($res); print "Ticker:"."\r\n"; foreach ($res['data'] as $value){ if (isset($value['story'])){ echo $value['story']."\r\n"; } } ?> 

Where

 <?php function get_friends_news($user_id ='me',$token)<br/> { $url = $this->url_base_graph.$user_id.'/home?access_token='.$token; $res = json_decode($this->httpQuery($url),true); return $res; } ?> 

and

 <?php function httpQuery($url, $method = 'GET', $post_data = array(), $CONNECTTIMEOUT = 30) { // type of query if ($method == 'POST') $method = 1; elseif ($method == 'GET') $method = 0; if ($this->access_token != false) $url = $url . 'access_token=' . $this->access_token; //echo $url; //traverse array and prepare data for posting (key1=value1) if (count($post_data)) { foreach ($post_data as $key => $value) { $post_items[] = $key . '=' . $value; } //create the final string to be posted using implode() $post_string = implode('&', $post_items); } else { $post_string = ''; } // echo $url; //create cURL connection $curl_connection = curl_init($url); //set options curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, $CONNECTTIMEOUT); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl_connection, CURLOPT_URL, $url); curl_setopt($curl_connection, CURLOPT_POST, $method); //set data to be posted if ($post_string != '') { curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); } //perform our request $result = curl_exec($curl_connection); //close the connection curl_close($curl_connection); return $result; }?> 
0
source

Source: https://habr.com/ru/post/1403726/


All Articles