Twitter Trends PHP - php

I am trying to use http://api.twitter.com/1/trends/current.json?exclude=hashtags however I am having some trouble.
So, I'm trying to use:
<?php
$init = 'http://api.twitter.com/1/trends/current.json?exclude=hashtags';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$init);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);
foreach ($obj[0]['trends'] as $trend) {
print $trend['query'];
echo "<br>";
print $trend['name'];
echo "<hr>";
}
?>
And I am getting this Error:
Notice: Undefined offset: 0 in \index.php on line 11
Warning: Invalid argument supplied for foreach() \index.php on line 11

You read the JSON wrong. You need to do something like the following:
foreach ($obj['trends']['2011-02-23 18:00:00'] as $trend) {
print $trend['query'];
echo "<br>";
print $trend['name'];
echo "<hr>";
}

If I am not wrong
$obj = json_decode($result, true);
would produce an array and not an object. secondly using the twitter api is as simple as:
<?php
function get_trends($woeid){
return json_decode(file_get_contents("http://api.twitter.com/1/trends/".$woeid.".json?exclude=hashtags", true), false);
}
$data = get_trends(23424848); //23424848 is woeid for India...
$trends = $data[0]->trends;
echo "<ul>";
if(!empty($trends)){
foreach($trends as $trend){
echo '<li>'.$trend->name.'</li>';
}
}
echo "</ul>";
?>

Related

nse option chain json response in table format

i have the code which is giving json response in table format using curl php. but i dont know how to display it as correct order like expiryDate uninterest for ce ChangeinopenInterest for ce strike price openInterest for pe and changeinopenInterest For pe side.
Here is the code
JSON Data Table
your text
Key
Value
<?php
$url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY";
// Initialize curl`your text`
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// Return the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Make the request
$response = curl exec($ch);
// Close the curl session
curl close($ch);
// Decode the JSON data
$data = json_decode($response, true);
// Recursive function to display arrays in a table
function displayArray($data) {
foreach ($data as $key => $value):
echo "<tr>";
echo "<td>$key</td>";
if (is_array($value)) {
echo "<td>";
echo "<table border='1' style='padding-left: " . ($level * 20) . "px;'>";
displayArray($value);
echo "</table>";
echo "</td>";
} else {
echo "<td>$value</td>";
}
echo "</tr>";
endforeach;
}
display Array($data);
?>
</tbody>
</table>

PHP List items in json data in a table

I am doing some requests using php i am getting this json data from the url i am requesting from and i need to list the name and tag category of each different set of brackets or item out in html how would i go about this see my code below
<?php
$query = $_POST['query'];
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://api.spiget.org/v2/search/resources/" + $query);
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result = curl_exec($cSession);
curl_close($cSession);
$json = json_decode($result);
foreach ($json['items'] as $data)
{
echo "<h3>". $json['name']."</h3>";
echo "<h4>". $json['tag']."</h3>";
echo "\n";
echo "\n";
echo "\n";
};?>
JSON DATA
Warning: A non-numeric value encountered in C:\xampp\htdocs\request.php on line 7
Warning: A non-numeric value encountered in C:\xampp\htdocs\request.php on line 7
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\request.php on line 14
Those are the errors i get when the file is run
You define in your json loop as $data so you need to use it in displaying
<?php
$query = $_POST['query'];
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://api.spiget.org/v2/search/resources/" + $query);
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result = curl_exec($cSession);
curl_close($cSession);
$json = json_decode($result);
foreach ($json['items'] as $data)
{
echo "<h3>". $data['name']."</h3>";
echo "<h4>". $data['tag']."</h3>";
echo "\n";
echo "\n";
echo "\n";
};?>

Retrieving data from curl and json_decode

I am trying to extract the individual data strings from a curl and json_decode result, but cannot manage to target the array elements correctly:
API:
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
Result:
{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}
PHP:
echo 'Bid: '.$obj['result']['Bid'].'<br/>';
echo 'Ask: '.$obj['result']['Ask'].'<br/>';
echo 'Last: '.$obj['result']['Last'].'<br/>';
Also tried:
echo 'Bid: '.$obj['Bid'].'<br/>';
echo 'Ask: '.$obj['Ask'].'<br/>';
echo 'Last: '.$obj['Last'].'<br/>';
You need to add a true parameter to your json_decode
Like
json_decode($execResult, true);
Else you get a stdObject with the decoded data.
Then you should be able to use $obj['result']['Bid'] aso.
The use of json_decode has an optional second parameter - boolean - that either returns the data as an object or as an array.
{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}
$obj = json_decode( $execResult ); /* will return an object */
echo $obj->result->Bid;
$obj = json_decode( $execResult, true ); /* will return an array */
echo $obj['result']['Bid'];
Just ran this as the test..
$data='{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}';
$obj = json_decode( $data ); /* will return an object */
echo $obj->result->Bid;
$obj = json_decode( $data, true ); /* will return an array */
echo $obj['result']['Bid'];
which displayed:
0.043500030.04350003
Use this way:
echo 'Bid: '.$obj->result->Bid.'<br/>';
echo 'Ask: '.$obj->result->Ask.'<br/>';
echo 'Last: '.$obj->result->Last.'<br/>';
let
$json = '{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}';
$obj = json_decode($json, true);// decode and convert it in array
echo 'Bid: '.$obj['result']['Bid'].'<br/>';
echo 'Ask: '.$obj['result']['Ask'].'<br/>';
echo 'Last: '.$obj['result']['Last'].'<br/>';
Try using json_decode($string, true); as an array
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult, true);
echo 'Bid: '.$obj['result']['Bid'].'<br/>';
echo 'Ask: '.$obj['result']['Ask'].'<br/>';
echo 'Last: '.$obj['result']['Last'].'<br/>';
OR using json_decode($string); as an object
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
echo 'Bid: '.$obj->result->Bid.'<br/>';
echo 'Ask: '.$obj->result->Ask.'<br/>';
echo 'Last: '.$obj->result->Last.'<br/>';

What makes this jsessionid shows up on this PHP-code result?

I want to do parsing on this site: CiteSeerx Result.
I tried this:
<?php
include('simple_html_dom.php');
$url = 'http://citeseerx.ist.psu.edu/search?q=mean&t=doc&sort=rlv&start=0';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
$html = new simple_html_dom();
$html->load($curl_scraped_page);
foreach ($html->find('div.result h3') as $title) {
echo $title->plaintext . '<br/>';
}
echo '---<br>';
foreach ($html->find('div.result h3 a') as $link) {
echo '\'http://citeseeerx.ist.psu.edu' . $link->href . '<br>';
}
echo '---<br>';
foreach ($html->find('div.pubinfo') as $info){
echo $info->innertext. '<br>';
}
echo '---<br>';
foreach ($html->find('div.snippet') as $snippet){
echo $snippet->innertext. '<br>';
}
?>
It works and gives me what I want, it's just that, this jsessionid=... shows up on every single line of the $link results.
What do I do to make it disappear? I googled for addressing this problem, but all I find is the way to solve it with Java, not PHP.
Thanks.
<a class="remove doc_details" href="/viewdoc/summary;jsessionid=103B4C6E9ADA3C8B17DD64BD57238F9D?doi=10.1.1.160.3832">
because the href in the tag includes the jsession id part :)

Smarty 3 tpl: how to execute php plugin function that calls a smarty variable into the .tpl file?

inside .tpl:
{assign var="datasheet" value=$product->reference|escape:'htmlall':'UTF-8'}
... ...
{testsk}
function.testsk.php:
<?php
function smarty_function_testsk(){
$ch = curl_init ("http://www.domain.com/path/to/".$smarty->get_template_vars('datasheet')."/name.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match("/\s<div id=\"divname\">(.*)<\/div>/siU", $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
}
?>
obviously it doesn't work but with a given known variable function is good and tested
i also tried to allow php tag into smarty class, it allows but i'm not able to access smarty variable.
IT WORKS:
{php}
$ch = curl_init ("http://www.domain.com/path/to/3345674/name.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match("/\s<div id=\"divname\">(.*)<\/div>/siU", $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
{/php}
IT DOESN'T WORK:
{assign var="datasheet" value=$product->reference|escape:'htmlall':'UTF-8'}
{php}
$v1 = $this->get_template_vars('datasheet');
$ch = curl_init ("http://www.domain.com/path/to/".$v1."/name.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match("/\s<div id=\"divname\">(.*)<\/div>/siU", $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
{/php}
ERROR:
Fatal error: Using $this when not in object context in /var/www/vhosts/domain.com/httpdocs/folder/tools/smarty/plugins/block.php.php(23) : eval()'d code on line 2
I don't know why $smarty->get_template_vars('datasheet') fails here, but you can work around it by explicitly passing the parameter and reading with $inParam[]:
your.tpl file
{assign var="datasheet" value=$product->reference|escape:'htmlall':'UTF-8'}
... ...
{testsk datasheet=$datasheet}
function.testsk.php
<?php
function smarty_function_testsk($inParam, $inSmarty){
$ch = curl_init ("http://www.domain.com/path/to/".$inParam['datasheet']."/name.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match("/\s<div id=\"divname\">(.*)<\/div>/siU", $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
}
?>
[code not tested]
http://www.smarty.net/docs/en/plugins.functions.tpl
(Edited above to separate file contents. Below here is new)
I assumed smarty v3. Should work the similarly for v2.x.
In a smarty .tpl file inside {php} ... {/php} you are at global scope and use $smarty->get_template_vars('var_name'); instead of $this->get_template_vars('var_name');.
On second look at your original code, $smarty->get_template_vars() fails because $smarty is not defined in the function scope, so you get null (and a notice about undefined variable). Put "global $smarty;" as first line of your plugin function body or, better change the function's parameter declaration "function smarty_function_testsk($param, $smarty)" which defines $smarty as the instance of the current template object.

Categories