Kraken API with a PHP script causes HTML page to go blank - php

Does anyone see a problem with this HTML/PHP code? as soon as i added it, the page showed as blank, with the browser not reading any source code at all. Even if i comment it out with it still is blank!
<body>
<?php
function getInfo ($a)
{
$online = 'images/streamRing/online.png';
$offline = 'images/streamRing/offline.png';
$size = '20';
$array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($a)), true);
if ($array['stream'] != NULL)
{
$channelTitle = $array['stream']['channel']['display_name'];
$streamTitle = $array['stream']['channel']['status'];
$currentGame = $array['stream']['channel']['game'];
echo "<tr><td class='onlineStatus'><img src='$online' height='$size' width='$size' alt='Online' />Online</td>";
echo "<td>$channelTitle</td><td>$streamTitle</td></tr>";
}
else
{
echo "<tr><td class='onlineStatus'><img src='$offline' height='$size' width='$size' alt='Offline' />Offline</td>";
echo "<td>$a</td><td> </td></tr>";
}
}
?>
.... (later in the page...) ....
<table class="onlineList">
<th>
<td class="onlineStatus"><h3>Status</h3></td>
<td><h3>Streamer Name</h3></td>
<td><h3>Stream Title</h3></td>
</th>
<?php
$streamer_1 = 'xxxx';
$streamer_2 = 'yyyy';
getInfo($streamer_2);
getInfo($streamer_1);
?>
</table>
The php code was developed using Kraken API, which is demonstrated rather simply here:
http://www.incendiarymedia.org/twitch/status.php
Edit: I noticed and fixed the breaking error. I used double quotes within a php echo, which is..... bad! However, the code still has an error. The table shows the headers, then the individual cells are misaligned. Somehow the images for the first column show up to the LEFT of the first column header. I don't see why!

Because this is invalid table markup
<table class="onlineList">
<th>
<td class="onlineStatus"><h3>Status</h3></td>
<td><h3>Streamer Name</h3></td>
<td><h3>Stream Title</h3></td>
</th>
The <th> element behaves like a cell, but you are using it like a row instead of <tr>
Try this:
<table class="onlineList">
<tr>
<td class="onlineStatus"><h3>Status</h3></td>
<td><h3>Streamer Name</h3></td>
<td><h3>Stream Title</h3></td>
</tr>

Related

MySQL Results in Body of PHPMailer Email

I'm trying to echo the results of a MySQL query into the body of an email to send via PHPMailer, but am having difficulties. The query works as I successfully create the table on the page, but can't seem to get the assigning the table to a variable correct.
My Code:
$body = '<html>
<body>
<table>
<thead>
<tr>
<th>Food</th>
<th>Quantity</th>
<th>Category</th>
<tr>
</thead>
<tbody>'.
while($row = $resultOrderE->fetch(PDO::FETCH_ASSOC)){
echo '
<tr>
<td>$row['food']</td>
<td>$row['quantity']</td>
<td>$row['category']</td>
</tr>
';}.'
</tbody>
</table>
</body>
</html>';
The error I get is:
PHP Parse error: syntax error, unexpected 'while' (T_WHILE)
Any suggestions? Thanks!
you cant concate a WHILE loop to a string.
you have to loop through and ADD to the existing string per iteration of the loop
$html_string = '<html><body><table><thead><tr><th>Food</th><th>Quantity</th> <th>Category</th><tr></thead><tbody>';
while($row = $resultOrderE->fetch(PDO::FETCH_ASSOC)){
$html_string .=
'<tr><td>'.$row['food'].'</td><td>'.$row['quantity'].'</td><td>'.$row['category'].'</td></tr>';
}
// this will add the closing tags and now html_string has your built email
$html_string .= '</tbody></table></body></html>';
so the .= is the important part, it concates a string to the end of existing string

Validation Error: Bad value for attribute href on element a: Illegal character in query: not a URL code point

I am pulling database information and displaying it in a table, the content of the title column is linked to a dynamic page. It all works perfectly but when validating my output page with the W3C Validator I get the following error and have no idea how to fix it!
Bad value newpage.php?url= DATABASE INFORMATION for attribute href on element a: Illegal character in query: not a URL code point.
It then highlights the end tag for the a href ">".
<?php
echo"<table>
<tr>
<th>Title</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</table>";
if (mysqli_num_rows($SQLresult) == 0 )
{
echo 'No results found, please try again.';
}
else
{
while ($row = mysqli_fetch_assoc($SQLresult))
{
echo "
<table>
<tr>
<td><a href='newpage.php?url=".$row['title']."'>".$row['title']."</td>
<td>".$row['A']."</td>
<td>".$row['B']."</td>
<td>".$row['C']."</td>
<td>".$row['D']."</td>
</tr>
</table>";
}
}
?>
Any help would be greatly appreciated, thanks.
Not all characters are legal in URLs. Those that are not legal can be recoded using the percent sign and two hex digits. PHP's urlencode() function does that. Use urlencode() on the part that is to be the link.
$url = urlencode($row['title']);
...
<td><a href='newpage.php?url=".$url."'>".$row['title']."</td>
Alternatively, you could do this:
<td><a href='newpage.php?url=".urlencode($row['title'])."'>".$row['title']."</td>
You can find more information about legal characters in URL/URI in RFC2396: https://www.rfc-editor.org/rfc/rfc2396#section-2

Force PHP newline to respect current indentation level

I have been googling like crazy to find an answer to what I thought would have been a very common question to no avail...
I am using "\n" to break a table into a more readable structure when viewing source. Is there any way to get it to respect current indentation levels? For example:
Turn this:
<table>
<tbody>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</tbody>
</table>
Into this:
<table>
<tbody>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</tbody>
</table>
I am printing the tbody rows and cells out with the following code:
<?php
$first = true;
foreach ($data->rows as $row) {
if ($first) {
print "<tr class='highlight'>";
$first = false;
} else {
print "<tr>";
}
foreach ($row as $cell) {
print "<td>";
printf('%s', $cell);
print "</td>\n";
}
print "</tr>\n";
}
?>
<?php
$first = true;
foreach ($data->rows as $row) {
if ($first) {
echo "\t\t<tr class='highlight'>";
$first = false;
} else {
print "\t\t<tr>";
}
foreach ($row as $cell) {
print "\t\t\t<td>";
printf("\t\t\t\t%s", $cell);
print "</td>\n";
}
print "\t\t</tr>\n";
}
?>
I recommend you to write an identation counter, if you would like to do it dynamically. And remember to use double quotes for escape sequences, single quotes won´t work here (because it´s treated like a variable).
This is what google gave me.
You should concatinate the entire table in one php string, parse it through the function that can be found here:
http://www.daveperrett.com/articles/2007/04/05/format-xml-with-php/
and then simply echo $xml;
I just found it, I give no guarantee that this works as itended but it looks reliable.
The final HTML source have not to respect an indentation... Nobody see it (or you but don't get crazy with this).
Ok, sometimes this is also an obsession for me (one among many others ?), so you can do:
echo '
<tr>';
//Loop starts
echo '
<td></td>';
// Loop ends
echo '
</tr>';
Here you get a newline and your HTML indentation is independant from the PHP one.
Is it what you would ?

get variable php error

i am trying to load an xml file to php in order to display a table
// load SimpleXML
$d = $_GET['d'];
$books = new SimpleXMLElement('books.xml?cat=($d)', null, true);
echo <<<EOF
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Price at Amazon.com</th>
<th>ISBN</th>
</tr>
EOF;
foreach($books as $book) // loop through our books
{
echo <<<EOF
<tr>
<td>{$book->title}</td>
<td>{$book->author}</td>
<td>{$book->publisher}</td>
<td>\${$book->amazon_price}</td>
<td>{$book['isbn']}</td>
</tr>
EOF;
}
echo '</table>';
i am unable to get the Get variable return any data.hope you can help me.
This is a single vs double quote issue.
Change
'books.xml?cat=($d)'
to
"books.xml?cat=($d)"
...and read this again. Properly.
At the very least 'books.xml?cat=($d)' is not expanded. You need to use double quotes.

Is PHP timing out, or is it something else that's causing my script's results to vary after each execution?

I have a directory full of XML files. For each of these files I make a search at RIPE. For each search I do a couple of RegEx searches through the returned HTML code. But after a couple of loops, file_get_contents stop returning data, and all my operations after are done on an empty string.
I figured PHP may be timing out since these pages take a while to load. But wouldn't the script execution stop completely then? Instead all the loops finish and output their HTML code, though without content.
I'm also guessing there could be some sort of maximum requests pr second deal with PHP.
Could anyone here shed some light on this?
Thanks
Edit: To explain my title, a friend of mine and myself were running the script at the same time. That's why I'm guessing PHP sets a limit to how many requests it can send pr minute or something, because it seems PHP manages a varying number of loops before it stops returning data.
Edit: Added some code: (I figured it wasn't needed, due to my explanation of the problem)
<?php
set_time_limit(0);
include "pagebase.php";
$page = new pagebase();
$page->jQuery = true;
$page->formatDoc = false;
$page->addScript("javascript.js");
$page->addStylesheet("../codeclean.css");
$page->addStylesheet("stylesheet.css");
$page->title = "...";
$directory_path = "xml_documents";
$directory = scandir($directory_path);
$files = array();
foreach($directory as $string)
{
if(preg_match("/.*\.xml/", $string, $result) > 0)
array_push($files, $result[0]);
}
$content =
"
<table cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td colspan=\"7\">
<center><h2>...</h2></center>
</td>
</tr>
<tr>
<td class=\"header_cell\">Case ID</td>
<td class=\"header_cell\">Description (From RIPE)</td>
<td class=\"header_cell\">IP</td>
<td class=\"header_cell\">Fil</td>
<td class=\"header_cell\">Time</td>
<td class=\"header_cell\">Type</td>
</tr>
";
foreach($files as $index => $file)
{
$xml = simplexml_load_file("$directory_path/$file");
$id = trim($xml->Case->ID);
$ip = trim($xml->Source->IP_Address);
$title = trim($xml->Content->Item->Title);
$time = trim($xml->Source->TimeStamp);
$type = trim($xml->Source->Type);
$desc_result = array();
$info_result = array();
$RIPE_result = file_get_contents("http://www.db.ripe.net/whois?searchtext=$ip");
preg_match("/(?<=descr:)(\s*)(.*)/", $RIPE_result, $desc_result);
preg_match_all("/<pre>.*<\/pre>/sm", $RIPE_result, $info_result);
$info_result[0] = implode("", $info_result[0]);
if(count($desc_result) < 1) $desc_result[0] = "<font style=\"color:red\">No description found</font>";
else $desc_result[0] = trim($desc_result[0]);
$content .=
"
<tr id=\"info_row_$index\">
<td class=\"info_cell\">$id</td>
<td class=\"info_cell\">$desc_result[0]</td>
<td class=\"info_cell\">$ip</td>
<td class=\"info_cell\">$title</td>
<td class=\"info_cell\">$time</td>
<td class=\"info_cell\">$type</td>
</tr>
<tr id=\"expanded_row_$index\">
<td class=\"expanded_cell\" colspan=\"7\">
<div id=\"content_container_$index\">
<input type=\"button\" class=\"pastey_button\" rel=\"$index\" value=\"Get info\" />
<div id=\"RIPE_$index\">$info_result[0]</div>
</div>
</td>
</tr>
";
}
$content .=
"
<tr>
<td colspan=\"6\">Vi har totalt ".count($files)." henvendelser.</td>
</tr>
</table>
";
$page->body = $content;
$page->drawPage();
?>
Testing inline code blocks
i think RIPE has usage limits - you may be being locked out if you perform too many queries in a certain amount of time.
If by timing out you mean file_get_contents timing out I'm pretty sure that'll throw an error (or at least return false). As far as I know PHP doesn't have a number of HTTP requests it can fulfil per execution.
How many items are you talking about here? Have you checked the values for those items?
You could try using set_time_limit(0) but PHP should throw an error if PHP's reaching maximum execution time so you might not need that.

Categories