Tuesday 11 April 2017

Get Social feeds (FB, Twitter, Youtube).

Getting Youtube video id using php curl For getting videos from youtube channel 1. Create an youtube api on https://console.developers.google.com 2. Get your account KEY. 3. Now goes to any youtube channel and inspect element and search out for "data-channel-external-id" value of this variable is your CHHANELID. 4. Now your arguments list is $option = array( 'part' => 'snippet,id', 'channelId' => CHHANELID, 'key' => KEY, 'order' => 'date', 'maxResults' => 2 ); Here you can change maxresult values as your requirements 5. Now make php curl request $url = "https://www.googleapis.com/youtube/v3/search?" . http_build_query($option, 'a', '&'); $curl = curl_init($url); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $json_response = curl_exec($curl); curl_close($curl); $responseobj = json_decode($json_response); Now get your data from $responseobj. You can more explore at https://developers.google.com/youtube/v3/getting-started#partial Getting data from facebook page 1. Get fb PAGEID Go to facebook page, right click on cover photo, copy link address and paste in url and extract pageid from here for eg. https://www.facebook.com/MaxHealthcareHospitals/photos/a.10150717461986834.457095.93226131833/10154143692526834/?type=3 Now here 93226131833 is page id. 2. Get your ACCESS TOKEN After that you have to make curl request $url = "https://graph.facebook.com/" . FBPAGEID . "/feed?fields=picture,message,id,created_time,link&limit=2&access_token=" . ACCESS_TOKEN . ""; Now, here in url you can define fields parameter whatever you want and you can set limit how many content you want. $curl = curl_init($url); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $json_response = curl_exec($curl); curl_close($curl); $responseobj = json_decode($json_response); now you can get your data from $responseobj. Getting data from twitter 1. Create an api on twitter https://apps.twitter.com/app/new 2. Get your OAUTH_ACCESS_TOKEN OAUTH_ACCESS_TOKEN_SECRET COUNSMER COUNSMER_SECRET SCREEN_NAME Screen name is the name of twitter handle for which you want to get data 3. $url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; $oauth = array( 'screen_name' => SCREEN_NAME, 'count' => 2, 'oauth_consumer_key' => COUNSMER, 'oauth_nonce' => time(), 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => OAUTH_ACCESS_TOKEN, 'oauth_timestamp' => time(), 'oauth_version' => '1.0', ); $base_info = twt_build_base_string($url, 'GET', $oauth); function max_custom_block_build_base_string($baseuri, $method, $params) { $r = array(); ksort($params); foreach ($params as $key => $value) { $r[] = "$key=" . rawurlencode($value); } $base_url = $method . "&" . rawurlencode($baseuri) . '&' . rawurlencode(implode('&', $r)); return $base_url; } $composite_key = rawurlencode(COUNSMER_SECRET) . '&' . rawurlencode(OAUTH_ACCESS_TOKEN_SECRET); $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, TRUE)); $oauth['oauth_signature'] = $oauth_signature; $header = array(twt_build_authorization_header($oauth), 'Expect:'); $options = array( CURLOPT_HTTPHEADER => $header, // CURLOPT_POSTFIELDS => $postfields. CURLOPT_HEADER => FALSE, CURLOPT_URL => $url . '?screen_name=' . SCREEN_NAME . '&count=2' , CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_SSL_VERIFYPEER => FALSE ); $feed = curl_init(); curl_setopt_array($feed, $options); $json = curl_exec($feed); curl_close($feed); $twitter_data = json_decode($json); You can define limit of data in count field. function max_custom_block_build_authorization_header($oauth) { $r = 'Authorization: OAuth '; $values = array(); foreach ($oauth as $key => $value) { $values[] = "$key=\"" . rawurlencode($value) . "\""; } $r .= implode(', ', $values); return $r; }

No comments:

Post a Comment

Thanks!