Is there a way of parsing information from smarty? - smarty

I have three files, a weather.php file which contacts an API and returns weather to my weather.tpl file. However I want to take the contents of the weather.tpl file (which only contains the api response) and post it to another page, any ideas about how I can go about doing this? I'm using smarty version 3.1.33.
Here are my files.
// Weather.php
<?php
$ch = curl_init();
$location = 'MyLocation';
$apikey = 'MyApiKey';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://api.openweathermap.org/data/2.5/weather?q='.$location.'&appid='.$apikey);
$output = curl_exec($ch);
curl_close($ch);//Free up system resource
$output = json_decode($output, true);
echo "The weather in ".$location." for MusicWorks Festival will be ".$output['main']['temp'];
?>
//weather.tpl
{block name="weather"}
{/block}
//account.tpl (where I want the contents to end up)
<div>
{block name="weather"}The weather: {/block}
</div>
I have used smarty like this to display both pages.
{extends file="layouts/main.tpl"}
{block name="body"}

Related

I'cant Download Image of post from Facebook

I use the api graph and obtain an image the type:
https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/27972193_10209727361415594_5326795209597866457_n.jpg?oh=680574d073b0e54d60ed86f9143b05f0&oe=5B1CC7DD
The, I use this code por download the image:
$link_curl = curl_init($image);
curl_setopt($link_curl, CURLOPT_RETURNTRANSFER, true);
$bytes_image = curl_exec($link_curl);
curl_close($link_curl);
$image_name=date("YmdHis")."jpg";
$file = fopen( $directory_local . $image_name,'w');
fwrite($file,$bytes_imagen);
fclose($file);
The result is a file empty.
Help Please

Full youtube palylist using curl ( only half list is dispalying using normal curl )

click to check how the list is loading
i am using the following code to curl youtube but it is showing only top 100 videos in playlist how can i show full playlist videos. There are 180 videos in playlist
( i know about API but i want to use this without youtube API)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/playlist?list=PL1FO3JrU9Zy-A-q8s4FdzgzNVWsIcTx5q');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$contents = curl_exec ($ch);
echo $contents;
curl_close ($ch);
?>
Which procedure should i follow to get the full playlist ?
I have got my answer.
I need to curl the remaining videos by
Firstly Gather information like url, parameters and request type (post/get) from that ajax request.( by clicking on inspect and the click on network leaflet)
Generate the same request from your php/curl code and you got it.

Codeigniter Curl

I am new to CI. I am uploading file from a url(drop box file chooser) but now its uploading to the root directory. I want to know how I can give path to file upload inside the CURL functions.
below is the code .
$curl = curl_init($url);
$file = fopen(basename($url), 'wb');
$file_name =(basename($url));
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_exec($curl);
curl_close($curl);
fclose($file);
Thank you.
That is not possible to set file upload path on curl functon direct.
Please use of drop box api library in codeigniter link below.
drop box api :- https://github.com/jimdoescode/CodeIgniter-Dropbox-API-Library
and follow step.

xpath giving wrong output in simple html dom

I'm trying to scrape every line on this url
http://www.gosugamers.net/counterstrike/news/archive
I've used xpath-helper to create following path:
//div[class='content']/table[#class='simple gamelist medium']/tbody/tr
this should print every line in the tbody however when i try this in simple html dom, it returns the thead with the title, date and comment. How come it does not return the tbody instead as it does in xpath helper?
include('simple_html_dom.php');
function getHTML($url,$timeout)
{
$ch = curl_init($url); // initialize curl with given url
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
return #curl_exec($ch);
}
$html = str_get_html(getHTML("http://www.gosugamers.net/counterstrike/news/archive",10));
$table = $html->find("//div[class='content']/table[#class='simple gamelist medium']/tbody/tr",0);
echo $table;
Update:
The simplehtmldom library doesn't appear to support positional predicates in XPath. To get a specific row, you need to pass a 0-based index as the second parameter to find().
To get the first non-header row (the second table row):
$table = $html->find("//div[class='content']/table[#class='simple gamelist medium']/tbody/tr", 1);
working phpfiddle
You XPath expression is selecting each `tr` element. If you want the whole `tbody` element, remove `/tr` from the end of the expression.
If you only want the table cells (`td`), add `/td[1]`.
If you only want the titles, add `/td[1]/a/string()`.

Load text from specific external DIV using AJAX?

I'm trying to load up the estimated world population from http://www.census.gov/ipc/www/popclockworld.html using AJAX, and so far, failing miserably.
There's a DIV with the ID "worldnumber" on that page which contains the estimated population, so that's the only text I want to grab from the page.
Here's what I've tried:
$(document).ready(function(){
$("#population").load('http://www.census.gov/ipc/www/popclockworld.html #worldnumber *');
});
What you are trying to do is known as a cross-domain request. This is not a feature that browsers normally allow (security feature). Some ways to get around this limitation are described here: The jQuery Cross-Domain Ajax Guide.
you can try something like this:
$.get('http://www.census.gov/ipc/www/popclockworld.html', function(content) {
$("#population").html($('#worldnumber',$(content)));
});
Yeah, it's security. You can't ajax in to pages that aren't from the same domain.
#R0MANARMY:
I couldn't seem to follow the directions given on that site you linked to, but I did figure out a solution... I created a PHP file with the following code:
//Run cURL call
$ch = curl_init('http://www.census.gov/main/www/rss/popclocks.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
//Set as new XML object
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
function parseRSS($xml) {
$cnt = count($xml->channel->item);
for($i=0; $i<$cnt; $i++) {
$title = $xml->channel->item[$i]->title;
if ( preg_match("/world population estimate:\s([0-9,]+)\s/i", $title, $match) ) {
echo $match[1];
}
}
}
parseRSS($doc);
Then I called it with jQuery like so:
<div id="population"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#population').load('getpop.php');
var refreshId = setInterval(function() {
$('#population').load('getpop.php');
}, 120000);
});
</script>
Just thought I'd post it here in case anyone else is looking to do something similar.

Resources