Using yql and php to clean up content
tricked YQL by trying to understand it a little better. I was able to get the information I want from an external site and get the node results in the YQL console, but that way was unable to display the results on the local development server.
What I ultimately want to do is try and put this in a wordpress function so that I can call it on a page (like a readings page).
The code I used in php (edit :: I changed the code to this)
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
$yql_base_url ="http://query.yahooapis.com/v1/public/yql";
$yql_query = 'SELECT * FROM html WHERE url="http://www.nwjhl.com/leagues/standingsTotals.cfm?leagueID=15654&clientID=4594" AND xpath=\'//table[@class="yuiboxscores"]/tbody/tr\'';
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json";
// set up the cURL
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $yql_query_url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
// execute the cURL
$rawdata = curl_exec($c);
curl_close($c);
// Convert the returned JSON to a PHP object
$data = json_decode($rawdata);
I managed to get the data I'm looking for now. I'm just having trouble transferring data as I'm accepting it because "tr" is in an array.
I was probably looking at it, probably a bit tricky to compute with YQL for me, I believe a for loop is needed, I just don't know how to refer to [content] and then of course all other numbers. I tried to debug with
$results = $data->query->results->td;
echo '<pre>' . print_r($results) . '</pre>';
I'm not sure how I could loop through all of these objects into arrays to display [content] (that's a command) and then of course statistics. By continuing to work on this, I hope I can understand
source to share
json that you return from that url is not what you expect from it. Try
echo $output
immediately after curl_close ($ ch);
then run it through json formatter (google it). $ data-> query-> works but there is no Result child object, it just looks like a jsonified html table.
Once you see the JSON in a more readable format, your problem might make more sense to you.
Per your edit - try this after json_decode the raw data. I'm not sure exactly what pieces of data you are after, but I guess it will be in the objects you dump with var_dump
$topics = $data->query->results->table->tbody->tr;
foreach ($topics as $topic)
{
echo "row:";
var_dump($topic);
}
Here's a more concrete example of how to loop through and find items
$topics = $data->query->results->table->tbody->tr;
foreach ($topics as $topic)
{
$data = $topic->td;
foreach ($data as $element)
{
if (array_key_exists('a', $element))
{
echo "Team Name: " . $element->a->content . "\n";
}
if (array_key_exists('p', $element))
{
echo "Found P: " . $element->p . "\n";
}
}
}
source to share