unable to grab the values of twitter and feedburner - php

$rssfeed = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=tutorialsbucket";
$twitter = "http://twitter.com/users/show.xml?screen_name=tutorialsbucket";
function followers($arg1, $arg2) {
$url = array($arg1, $arg2);
foreach ($url as $value)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $value);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
$tw = $xml->followers_count;
if (!empty($fb) || !empty($tw))
{
return $fb;
return $tw;
}
}
}
hi friends,
I am writing code to grab values of my twitter and Feedburner.but its not going good. i want to extract both values alone but i am unable to do this.

You can make followers() process one of the feeds and call the function twice, or you can return an array with both values.
$rssfeed = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=tutorialsbucket";
$twitter = "http://twitter.com/users/show.xml?screen_name=tutorialsbucket";
function followers($url, $twitter) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
if($twitter) {
return $xml->followers_count;
}else{
return $xml->feed->entry['circulation'];
}
}
$tw = followers($twitter, true);
$fb = followers($rssfeed, false);
or:
$rssfeed = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=tutorialsbucket";
$twitter = "http://twitter.com/users/show.xml?screen_name=tutorialsbucket";
function followers($arg1, $arg2) {
$url = array($arg1, $arg2);
$result = array();
foreach ($url as $value)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $value);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
$tw = $xml->followers_count;
if (!empty($fb))
{
$result[] = $fb;
}elseif(!empty($tw)){
$result[] = $tw;
}
}
return $result;
}
list($fb, $tw) = followers($rssfeed, $twitter);
I prefer the first one since it's simple and obvious what happens. You can make it even more general by using XPath to fetch the entries and letting the second argument be an XPath description.

Related

Returning data from controller - Codeigniter

I am trying to return data to my variable $item_id but the variable returns empty. Am i not returning the data the right way ??
item_id should return "Alert" which is coming from the index function in serverMonitor.php.
Please advice. Thank you
Items.php
function rest_state()
{
$id = 1;
$item_id = $this->pass_to_res($id);
return $item_id //this should return "Alert"
}
function pass_to_res($id)
{
$url = "https://example.com/serverMonitor?id=$id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
serverMonitor.php
function index() {
$id = htmlentities($_GET['id']);
$word = "Alert";
return $word;
}
Return values from a function are not always visible to the visitor. You should use echo, print or print_r.
function index() {
$id = htmlentities($_GET['id']);
$word = "Alert";
echo $word;
return true;
}
You can also use JSON with json_encode to encoding
function index() {
$id = htmlentities($_GET['id']);
$word = "Alert";
echo json_encode(['word' => $word]);
return true;
}
And json_decode to decoding:
function pass_to_res($id)
{
$url = "https://example.com/serverMonitor?id=$id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
curl_close ($ch);
$result = json_decode($result);
return isset($result['word']) ? $result['word'] : false;
}

PHP: JSON Api Tenor

How can I get all url-s from https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8 (results->media->nanomp4->url)
if($json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8")) {
$obj = json_decode($json);
echo $obj->results->{"media"}->{"nanomp4"}->{"url"};
}
Function cURLGetContents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
On a circulate basis:
$json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8");
$urlList = getAllUrls((array) json_decode($json, true));
function getAllUrls($input, $list = []) {
foreach ($input as $key => $data) {
if (is_array($data)) {
$list = getAllUrls($data, $list);
} elseif ($key === 'url') {
$list[] = $data;
}
}
return $list;
}
$endereco = "https://api.tenor.com/v1/search?key=MBDPHCT6LA4H&q=sexo&limit=2";
$GrabURL = cURLGetContents($endereco);
$searchResponse = json_decode($GrabURL, true);
foreach ($searchResponse["results"] as $searchResult) {
print_r($searchResult["media"][0]["tinygif"]["url"]);
}
function cURLGetContents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

"Invalid argument supplied for foreach()" reading XML?

IM gettin: "Invalid argument supplied for foreach()"
in the for each line when trying to read an specific item of the xml
PHP:
$urls="http://www.floatrates.com/daily/usd.xml";
$url= $urls;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$feed = new SimpleXMLElement($data,LIBXML_NOCDATA);
foreach ($feed->channel->item as $item) {
$title = $item->title;
if (preg_match('/ARS/',$title)) {
echo $title;
}
}
$urls = "http://www.floatrates.com/daily/usd.xml";
$url = $urls;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$feed = new SimpleXMLElement($data, LIBXML_NOCDATA);
foreach ($feed->children() as $item) {
if ("item" == $item->getName()) {
$title = $item->title;
if (preg_match('/ARS/', $title)) {
echo $title;
}
}
}
It probably means that $feed does not contain what you expect it to contain, and you are not passing array to foreach Use print_r($feed); to see that it actually contains, and go from there.

Codeigniter recursive function not returning value

I have issues with codeigniter recursive function which returns blank value. below is my function.
function recursive_data($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$instaArr = (array) json_decode($output);
// print_r($instaArr); // print value here
if( $instaArr['pagination'] == "" ) {
return $instaArr['data'];
} else {
return $this->recursive_data($instaArr['pagination']->next_url);
}
}
I am calling this above function in another function like this
$return_data = $this->recursive_data($url);
It is returning blank value. while it is printing the value in commented code print_r($instaArr)
please use it as bellow.
function recursive_data($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$instaArr = json_decode($output,true);
// print_r($instaArr); // print value here
if( $instaArr['pagination'] == "" ) {
return $instaArr['data'];
} else {
return $this->recursive_data($instaArr['pagination']->next_url);
}
}
What about this ?
function recursive_data($url)
{
$objCurlData = new stdClass();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$instaArr = (array) json_decode($output);
// print_r($instaArr); // print value here
if( $instaArr['pagination'] == "" ) {
$objCurlData->data = $instaArr['data'];
} else {
$objCurlData->objChild = $this->recursive_data($instaArr['pagination']->next_url);
}
return $objCurlData;
}

Handling cURL error inside a function?

I've seen the documentation, and several tutorials. However I can't get my code work handling errors. I just want to print "Error.", whatever error is.
Here's my code without handling error,
function get_data($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$page = get_data('http://....');
$doc = new DOMDocument();
#$doc->loadHTML($page);
echo $doc->saveHTML();
EDIT: The next one prints the default error message, but not mine.
function get_data($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
if($data === FALSE) {
$msg = curl_error($ch);
curl_close($ch);
throw new Exception($msg);
}
curl_close($ch);
return $data;
}
try {
$page = get_data('wrong-url');
$doc = new DOMDocument();
#$doc->loadHTML($page);
$div = $doc->getElementById('cuerpo');
echo $doc->saveHTML($div);
} catch (Exception $e) {
echo 'Error ', $e->getMessage();
}
curl_exec() will return false on errors. You can throw an Exception in this case:
function get_data($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
if($data === FALSE) {
$msg = curl_error($ch);
curl_close($ch);
throw new Exception($msg);
}
curl_close($ch);
return $data;
}

Categories