Symfony 2.5 server returns 500 status - php

When I'm doing a request into API that I've written using Symfony 2.5. I'm sending form data using POST method and in the controller I'm doing
$params = json_decode($request->getContent(), true);
and if I do
var_dump($params);
I can see everything in console If I'm doing
console.log(data)
But If I try just do something like this:
array_keys($params)
Server returns me a 500 status code and error:
XMLHttpRequest cannot load http://host.loc/app_dev.php/posts. Invalid HTTP status code 500

You will always get 500 Error if something went completely wrong.
e.g. even use dump() in twig - which is not allowed in production
1) try the same page in /app_dev.php/[page]
- enable debug, this should give you alot of info about the error
2) look at the correct log file /app/logs/ as mentioned above
I guess you calling getContent() on a null Object ;-)

Related

Why am I always getting "PubnubException"?

I trying to build a server using laravel and pubnub, but the page always show a blank page, and after i look at the response it seems there is something wrong on pubnub.php (http response : 500)
and after I open dist/pubnub.php, it always said:
"PubnubException in Pubnub.php line 93: Missing required $subscribe_key param"
and after some searching, I found that I need to get a pub_key and sub_key, now for the question, what should I do to fix this error? how to init the pubnub? and of course I have search something about this but all of them still lead me to a dead end.
Register at pubnub.com, get your keys and use them when init:
This is the format:
$pubnub = new Pubnub($publish_key, $subscribe_key, $secret_key)
For more details:
https://www.pubnub.com/docs/php/pubnub-php-sdk#init

POST 500 (Internal Server Error) - Ajax & laravel 4

Sorry if my explanation is bad. Anyways, I have this todo list made by using laravel 4 sometime last year. Been working great for the last few months and I stopped playing around with it. Somehow I was playing around with it just yesterday and now it's giving me this POST 500 (internal Server ERROR). The Ajax call was working perfectly before. I had this error with chrome last night and today I tried using firefox, somehow it worked but then few minutes after, I am getting the same error in firefox too. By the way, a few ajax call is used here. Adding an item uses ajax to post what's added with a done button (this ajax is working perfectly). When the done button is clicked it should become a delete button and this is the ajax that's giving me the error. I went to the app.php to turn the debug => true
This error message is shown
{"error":{"type":"ErrorException","message":"Creating default object from empty value","file":"\/home2
\/.........\/TodoController.php","line":174}}
In my line 174 I have $query->done = 1;
that line is inside of this function
public function doubleDQuery($table, $id, $todoText, $done, $action){
$query = $table::where('id', '=', $id)->where('todoText', '=', $todoText)
->where('done', '=', $done)->first();
if($action == 'done'){
$query->done = 1;
$query->touch();
$query->save();
}
if($action == 'delete'){
$query->delete();
}
}
if I commented out the $query->done = 1, it gives me an error to the next line
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member
function touch() on a non-object","file":"\/home2\......\/TodoController
.php","line":175}}
if I commented the above line up, it gives me the error of none-object to save().
No modification is done for the past months and it was working for like a minute in firefox as mentioned above. I just have no idea where I should start looking for an answer, can someone give me a hand where I should start looking?
I have read that since 500 is internal server error it might be the server instead of my codes but it's weird that the other ajax call in the same application works just this is giving me the headaches.
That just means that $query is not what you think it is.
It seems to be null which means that there is no entry in the database matching your where clause. Although you expect it, respectively require it to be an object and also an instance of your model class for this code to work, without throwing an exception.
So you can either check for null manually and do whatever you wan't like:
if($query === null) {
//No matching row found
}
Or use firstOrFail() instead of first() which will actually already throw an exception with a 404 status code which may be what you want because you probably want to handle it properly on the client side.

always returns 200 as a status code from zend controller

I am creating custom class to throw a error from zend as there is not built-in mechanism for this in zend. I am able to give proper response to the user. But I am having problem in returning status code for this.
$obj = $this->toJsonModel($result);
$this->getResponse()->setStatusCode(403);
error_log('Status Code' . $this->getResponse()->getStatusCode());
return $obj;
I am getting response in $obj in Json format. Then I am setting status code to 403 for this API which I created. And then I am returning $obj from this controller.
But I am not getting 403 error when I tried this code. It's always showing 200 as a status code.
Some other code was causing a problem in executing this and was giving 200 every time but when I re-did some, it started working.

How to catch error in JSON response?

I'm getting the json data from the twitter using the wordpress function wp_remote_get as below:
$response = wp_remote_get('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter');
$json = json_decode($response['body']);
and it works fine. However, it totally creates the problem on my page if the twitter returns some error. In that case the content on my page does not load after that error part. So how can I catch the error and continue, only if there is no error.
For example if the twitter limit exceeds, it returns following response:
{"request":"\/1\/statuses\/user_timeline.json?screen_name=twitter","error":"Rate limit exceeded. Clients may not make more than 150 requests per hour."}
From the above responsive, how can i get the error.
I'm trying following, but it does not work (my page does not load at all)
if (!isset($json['error']){
//continue
}
It doesn't work because you are trying to access $json as an array, which it is not. It's an object, so use:
if (!isset($json->error){
//continue
}
Your page doesn't load because it's causing a fatal error, which in turn produces a 500 internal server error. If you check your error log you'll find:
Fatal error: Cannot use object of type stdClass as array in ....
You need to use json_decode to "transform" your "json string" in an object.
$j = json_decode($json);
echo $j->{'error'}
Here you will find everything what you need to know about json_decode: http://php.net/manual/en/function.json-decode.php

PHP and AJAX - internal error 500

I am trying to make a simple AJAX calling in PHP and have a problem with Internal error 500. When I click on the link, so the data are successfully loaded - this I see through FireBug, but I got the error above. It's on localhost.
When I try to set to the URL of a browser the address, that is called by AJAX, so the content is successfully loaded.
Where I should start to search the problem - or what could be wrong?
EDIT: in apache log is nothing weird, looks fine.
If after checking your php error log you don't find any issues, could it be your javascript AJAX call expects results to be returned in a specific format like JSON?
If this is the case, you need PHP to set the correct content type header when it is responding to the AJAX call, like so:
header('Content-type: application/json');
So in context, this might look something like:
$some_data = array(
'user_id' => 47,
'first_name' => 'Mike',
);
header('Content-type: application/json');
echo json_encode($some_data);
Look in the PHP error logs, or perhaps even your web server's error log.
Or if your script spews out errors to the client (which it hopefully doesn't do, at least not in production), try taking a look at the response in Firebug.
You most likely have a bug in your PHP. It doesn't matter if it is an AJAX call. Look at your server logs.

Categories