Print out JSON with PHP - php

Nothing shows on the screen, is this valid code below? I know theres a JSON parameter called 'text' within the received data but not sure how to print it out?
<?php
$url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
//print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
$obj = json_decode($data);
print $obj->{'text'};
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

This should work:
$obj = json_decode(get_data($url));
$text = $obj[0]->text;
It's a good idea to try something like var_dump($obj) when you run into an issue like this. After doing so, it becomes immediately clear $obj[0]->text is what you're after.
#benhowdle89 comment:
foreach ($obj as $item) {
$text = $item->text;
}

You should assign the value returned by get_data to a variable and pass it to json_decode i.e.:
<?php
$url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
//print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
$data = get_data($url);
$obj = json_decode($data);
print $obj->text;
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

$data is not set, and you don't need the curly braces.

Related

Roblox API ( Avatar Equipment )

I am currently working on Roblox API. I am stuck on one question. Reason :
I have this link https://avatar.roblox.com/v1/users/2/currently-wearing.
This shows what specified users have equipped on them. this link right here shows this:
{"assetIds":[382537569,607702162,607785314]}
My goal is to get the assetIds to string.
I tried this:
<?php
$id = 2;
$ch = file_get_contents("https://avatar.roblox.com/v1/users/$id/currently-wearing");
$ch = curl_init($ch);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$data = json_decode($data);
$id = "$data->assetIds";
echo $id;
But it Shows Array. I need some help.
The cURL is not necessary as the file_get_contents() get you the data you appear to require
$id = 2;
$ch = file_get_contents("https://avatar.roblox.com/v1/users/2/currently-wearing");
$data = json_decode($ch);
foreach ($data->assetIds as $id){
echo $id . PHP_EOL;
}
RESULT
382537569
607702162
607785314

Use curl in a loop to send data to the clients from the numbers fetched from database

$text="text.";
//fetch from DB
$sql=mysql_query("SELECT * FROM `employee`");
while($query=mysql_fetch_array($sql))
{
echo $employee_mobile=$query['employee_mobile'];
echo $url="http://mobile.ssexpertcompu.com/vendorsms/pushsms.aspx?user=MYUSERNAME&password=MYPASSWORD&msisdn=".$employee_salary."&sid=78NSL&msg=".$text."&fl=0&gwid=2";
$url = str_replace(" ","%20",$url); // to properly format the url
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
echo $data = curl_exec($ch);
curl_close($ch);
}
First employee record are fetched from DB,then using curl I'm sending message to their respective mobile numbers.The code for sending messages works fine without loop. I've seen some post how to use curl in a loop, but couldn't modify to my needs.
Looks like you have not defined $employee_salary and should exchange it with $employee_mobile:
echo $employee_mobile = $query['employee_mobile'];
echo $url = "http://mobile.ssexpertcompu.com/vendorsms/pushsms.aspx?user=MYUSERNAME&password=MYPASSWORD&msisdn=" . $employee_mobile . "&sid=78NSL&msg=" . $text . "&fl=0&gwid=2";

Get location from a stream url in PHP

I'm trying to get the redirect url from a stream using php.
Here's the code I have right now:
<?php
$stream = 'https://api.soundcloud.com/tracks/178525956/stream?client_id=XXXXXX';
$ch = curl_init($stream);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = curl_exec($ch);
echo $url;
curl_close($ch);
?>
Which outputs as a string:
{"status":"302 - Found","location":"THE_URL_I_WANT"}
So how ould I go about getting the url I want as a variable?
Thanks
It's simple use json_decode
$data = json_decode($url);
$your_url = $data->location;
How about
$data = json_decode($url);
$location = $data["location"];

Php webscraping using simple html dom not working when output is out of order html tags

I want to scrap some information of a webpage .It uses a table layout structure.
I want to extract the third table inside the nested table layout which contains a series of nested tables .Each publishing a result .But the code is not working
include('simple_html_dom.php');
$url = 'http://exams.keralauniversity.ac.in/Login/index.php?reslt=1';
$html = file_get_contents($url);
$result =$html->find("table", 2);
echo $result;
I Used Curl to extract website but the problem is its tags is in out of order so it cannot be extracted using simple dom element .
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL,$url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
function scrape_between($data, $start, $end){
$data = stristr($data, $start); // Stripping all data from before $start
$data = substr($data, strlen($start)); // Stripping $start
$stop = stripos($data, $end); // Getting the position of the $end of the data to scrape
$data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape
return $data; // Returning the scraped data from the function
}
$scraped_page = curl($url); // Executing our curl function to scrape the webpage http://www.example.com and return the results into the $scraped_website variable
$scraped_data = scrape_between($scraped_page, ' </html>', '</table></td><td></td></tr>
</table>');
echo $scraped_data;
$myfile = fopen("newfile.html", "w") or die("Unable to open file!");
fwrite($myfile, $scraped_data);
fclose($myfile);
How to scrape the result and save the pdf
Simple HTML Dom can't handle that html. So first switch to this library,
Then do:
require_once('advanced_html_dom.php');
$dom = file_get_html('http://exams.keralauniversity.ac.in/Login/index.php?reslt=1');
$rows = array();
foreach($dom->find('tr.Function_Text_Normal:has(td[3])') as $tr){
$row['num'] = $tr->find('td[2]', 0)->text;
$row['text'] = $tr->find('td[3]', 0)->text;
$row['pdf'] = $tr->find('td[3] a', 0)->href;
if(preg_match_all('/\d+/', $tr->parent->find('u', 0)->text, $m)){
list($row['day'], $row['month'], $row['year']) = $m[0];
}
// uncomment next 2 lines to save the pdf
// $filename = preg_replace('/.*\//', '', $row['pdf']);
// file_put_contents($filename, file_get_contents($row['pdf']));
$rows[] = $row;
}
var_dump($rows);
Find a sample code
?php
// Defining the basic cURL function
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
?>
<?php
$scraped_website = curl("http://www.example.com"); // Executing our curl function to scrape the webpage http://www.example.com and return the results into the $scraped_website variable
$result =$substring($scraped_website ,11,7); //change values 11,7 for table
echo $result;
?>

Retrieve all languages used by a Github user

In my script I have a function that retrieves JSON information from the Github API, https://api.github.com/users/octocat/repos.
I want to have a different function to get all the languages used by (in this case) octocat and then count how many times he used the language.
I was thinking of this:
foreach($json['language'] as $RepoLanguage)
{
echo $RepoLanguage;
}
but that won't work, any suggestions/ideas?
I think the main reason is that you did not specify the User Agent as specified here: https://developer.github.com/v3/#user-agent-required
Did you check what result you have in the $json?
Here's a working example.
<?php
function get_content_from_github($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
curl_setopt($ch,CURLOPT_USERAGENT,'My User Agent');
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$json = json_decode(get_content_from_github('https://api.github.com/users/octocat/repos'), true);
foreach($json as $repo) {
$language = $repo['language'];
}
?>

Categories