Cakephp 2.0: Errors with Google Weather API + Xml - cakephp-2.0

I have created an Element for my project which loads the Google Weather API. Locally it works perfectly. The problem appeared when I uploaded it and when I gave it to a friend of mine to try it.
The code for the element is the following one
<?php
Configure::write('debug', 2);
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=name_city');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
<html>
<head>
<title>Google Weather API</title>
</head>
<body>
<h1><?= print $information[0]->city['data']; ?></h1>
<h2>Today's weather</h2>
<div class="weather">
<?php $icon_today = (string) $current[0]->condition['data'];?>
<?php echo $this->Html->image('tick/'.$icon_today.'.png');?>
<span class="condition">
<?= $current[0]->temp_c['data'] ?>° C,
</span>
</div>
<h2>Forecast</h2>
<? foreach ($forecast_list as $forecast) : ?>
<div class="weather">
<?php $icon = (string) $forecast->condition['data'];?>
<?php echo $this->Html->image('tick/'.$icon.'.png',array('width' => '45'));?>
<div><?= $forecast->day_of_week['data']; ?></div>
<span class="condition">
<?php
$low = (int) $forecast->low['data'];
$high = (int) $forecast->high['data'];
$low_celsius = (int) (($low - 32) * (5/9));
$high_celsius = (int)(($high - 32) * (5/9));
?>
<?= $low_celsius ?>° C - <?= $high_celsius ?>° C,
<?= $forecast->condition['data'] ?>
</span>
</div>
<? endforeach ?>
</body>
</html>
When I uploaded it I got these messages
Warning (2): simplexml_load_file() [function.simplexml-load-file]:
http://www.google.com/ig/api?weather=name_city: parser error : Document is empty
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]:
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]: ^
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]:
http://www.google.com/ig/api?weather=name_city:1: parser error : Start tag
expected, '<' not found [APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]:
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]: ^
[APP/View/Elements/weather.ctp, line 5]
Fatal error: Call to a member function xpath() on a non-object in
/home/wwwsite/public_html/testing/app/View/Elements/weather.ctp on line 6
name_city has a real value. When I tried it on another computer there where other types of error ($forecast->day_of_week['data'] for example was not recognized as a valid variable). It seems that the online version is not able to receive data from Google Weather so the result is empty. Could anyone please tell me what to do?

Google plans to shut down iGoogle and related APIs. Already stopped responding weather API from 27th August. You can use wunderground.com's api

There is currently an intermittent 403 Forbidden response to the Google Weather API that you are using. See Google Weather API 403 Error
The 403 response may be causing the empty document that you see in the first line of the results. Rerun the code again now both locally and on the server and see if the problem is intermittent.
The reason for the intermittent 403 response is not known but has been a problem since the 7th of August 2012 which is the time that you posted this problem.

Related

CakePHP4 not showing or logging fatal error

I have deployed my app to the server, and on refreshing the home page I get the generic "An error has occurred " page with no details.
There is no corresponding entry in the error.log
There is not corresponding entry in /var/log/php-fpm/www-error.log
Looking at phpinfo, i have
display_errors: on
display_startup_errors: on
log_errors: on
app.php has debug = true
I can't find any information about the error anywhere. How can I force Cake to show or log the error?
Did you run composer install ? Have you checked apache/nginx logs ?
I added this to error500.php
<div class="" style="padding: 20px">
<?= debug($error->getMessage()) ?>
<br/>
<strong>File</strong>
<?= h($error->getFile()) ?>
<br/>
<strong>Line: </strong>
<?= stackTrace() ?>
<?= $this->element('exception_stack_trace');?>
</div>
This showed the error, which was of course lack of write permissions for the appache group for error.log.

cakephp custom flash layout error

i have looked at the documentatioin of cakephp 2.5.4 about custom flash messages and some other tutorials on how to make this but its not working
i have error.ctp and success.ctp under elements folder
error.ctp
<div id="error-flash">
<?php echo h($message); ?>
</div>
success.ctp
<div id="success-flash">
<?php echo h($message); ?>
</div>
and in controller i call them like this
$this->Session->setFlash(__(' file successfully uploaded.','success',array('class'=>"flash_msg_ok")));
$this->Session->setFlash(__('Upload Failed','error',
array("class" => "flash_msg_error")));
my chrome developer tools still shows the default flash generated.
its not rendering the element or applying the specified classes
i've also tried this
$this->Session->setFlash('success','default',array('class'=> 'success'))
still nothing.
of note is that am using custom layout and css for the page.
what am i missing here?
There are a couple of issues..
First, you've accidentally put the other function variables inside the translation string. You can fix the call as follows (note closing bracket moved to end of message string):
$this->Session->setFlash(__(' file successfully uploaded.'),'success',array('class'=>"flash_msg_ok"));
$this->Session->setFlash(__('Upload Failed'),'error',
array("class" => "flash_msg_error"));
Second, you'll need to add the class to your custom error element file (the cakephp docs neglect to mention this!). For example:
<div id="error-flash" class="<?php echo $class; ?>">
<?php echo h($message); ?>
</div>
Hope that helps!

AppError 404 equivalent in cake 2.0

I've recently upgraded my 1.3 cake app to 2.0, and I'm trying to redo my app_error code.
With Cake 1.3, it was simply a case of creating an app_error.php file, putting it in my app root, and overriding the built-in error404() and missingController() actions.
Here is my old 1.3 /app/app_error.php file: http://pastebin.com/beWZD9PJ
It had some code that kicked in when someone arrived at the site with a predefined 'alias' URL, and then it redirected them accordingly.
I just need this to work in Cake2.0, and I see the manual is telling me its all changed, but I can't find a specific case like this. Can anyone help me out, so the error404 code kicks in?
Many thanks
4xx and 5xx errors in CakePHP 2.0 ar now exceptions, thus you need to customize your Exception renderer or handler. Read from this section to the bottom http://book.cakephp.org/2.0/en/development/exceptions.html#exception-renderer
You can throw a 404 exception with the following code in your controller in Cake 2:
throw new NotFoundException(__('Your error text here'));
Then, you just need to have APP/View/Errors/error400.ctp present with something relevant to display. Cake includes this by default:
<h2><?php echo $name; ?></h2>
<p class="error">
<strong><?php echo __d('cake', 'Error'); ?>: </strong>
<?php printf(
__d('cake', 'The requested address %s was not found on this server.'),
"<strong>'{$url}'</strong>"
); ?>
</p>
<?php
if (Configure::read('debug') > 0 ):
echo $this->element('exception_stack_trace');
endif;
?>
It's simple.
With cakephp 2, you have to add this function in your AppController:
public function appError($error) {
$this->redirect('/');
}
Here, for example we redirect all errors to index page.

Why doesn't my CakePHP flash message appear?

Background: I'm new to CakePHP. I have a small test site (mostly composted of static views and a contact form at the moment) to play with while I learn. Everything worked fine on localhost (Apache on Ubuntu) so I deployed it to a shared hosting account (provided by Lunarpages). I moved the /cake folder out of the normal directory structure (so I could use it for multiple apps) and I reconfigured my webroot's index.php paths accordingly.
Problems:
setFlash messages do not get displayed. Even making a simple view that does nothing other than $this->Session->setFlash('message');. I don't get any error message, the flash just doesn't get displayed
Redirects don't work. For instance, after the contact form is completed I want to $this->redirect( array( 'action' => 'success' ), null, true); but the server throws an error:
Warning (2): Cannot modify header information - headers already sent by (output started at /routetoapp/config/routes.php:40) [CORE/cake/libs/controller/controller.php, line 742]
Everything else seems to work just as it did on localhost - URL rewriting, component loading, model validation. I don't know if my problems are related or separate issues
Troubleshooting so far:
I've tried both 'cake' and 'php' for Configure::write('Session.save', 'val'); but neither made a difference.
My app/tmp folder is writeable.
My layout template has the correct code for displaying flash messages. (The exact same M, V, C, and Layout objects display the expected flash on localhost)
I assume I'm missing something simple, but I'm new to this framework so I'm not sure where else to look.
See Matt Huggins answer for your flash problem. That's correct
As for your redirect issue,
you might have an extra space or something in your routes.php file. Make sure there are no spaces before the starting <?php tag and remove the closing ?>
$this->Session->setFlash(...) is used to SET the flash message from within the controller. When you're in the view, you should be rendering the flash message like this:
<?php $session->flash(); ?>
You can also make your flash message more elaborate if you need with something like this:
<?php if ($session->check('Message.flash')): ?>
<div class="message">
<?php $session->flash(); ?>
</div>
<?php endif; ?>
That was right but there also have echo add before $session->flash();
so, it should be like:
<?php if ($session->check('Message.flash')): ?>
<div class="message">
<?php echo $session->flash(); ?>
</div> <?php endif; ?>
this worked for me!

cakephp + cache + formhelper inside <cake:nocache></cake:nocache> = error

in all my views i have a login/register (cake)form.
my question:
can i use the file cache engine with <cake:nocache>form</ cake:nocache>
1.) open url "www.domain.com/home"
2.) cachefile generated
3.) look perfect
4.) refresh (f5)
5.) error (when debug=1):
Parse error: parse error in C:\xampp\htdocs\cake_1.2.3.8166\app\tmp\cache\view \cake_1_2_3_8166_home.php on line 752
cachefile -> line 752 -> </html>
cakephp: 1.2.3.8166
example:
<cake:nocache>
<?
$user = $session->read("user");
if(!$user){ //$user true or false
echo "login:";
echo $form->create('AdminUser', array('action' =>'login_load'));
echo $form->input('email',array('label'=>false));
echo $form->input('password',array('label'=>false));
echo $form->submit('Login', array('id'=>'login'));
echo $form->end();
}else{
echo "hello user!";
}?>
</cake:nocache>
it's a cakephp bug. cake google groups
The dev state at https://trac.cakephp.org/ticket/6034 CakePHP does not support using the form helper from within tags at this time.

Resources