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

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.

Related

Facebook PHP API throwing array reference exception

I am re-asking this question. The original occurrence went away after retrying the post, but on Dec 9, it has happened again, with greater frequency. I now have over 140 requests backed up that are failing with this same error inside the Facebook PHP API, and retrying the posts still fails. Dumping out the value of $rawResponse header shows:
{"
Just a left brace followed by a double quote, so it looks like the original function response is probably some JSON. Not sure what function made the original call, looking into that if I can make this happen in my DEV environment.
So, I just got back from vacation, and find that the Facebook PHP API was throwing this error:
Undefined offset: 1 in /home/httpd/idxv3/lib/Facebook-v5/Http/GraphRawResponse.php on line 108
The line is in this function:
public function
setHttpResponseCodeFromHeader($rawResponseHeader)
{
preg_match('|HTTP/\d\.\d\s+(\d+)\s+.*|', $rawResponseHeader, $match);
$this->httpResponseCode = (int)$match[1]; // <---- HERE
}
Other than the dangerous programming, why is this suddenly happening?
Ok, I believe that I have determined the problem. I recreated my Facebook API post inside the debugger, and determined that there is a block on the particular page I am posting to - but Facebook is returning a block of JSON code, which the API can't seem to handle. So, my question is moot - the API isn't written to handle the return values it might be getting from Facebook. Lovely.

cURL returns half json string in most cases

First edit (more details):
I used Modify Headers plugin in Firefox and visited the page (http://api.wunderlist.com/me/tasks), this showed the full and correct json string that I need. It never fails (25+ tests)
I tried using file_get_contents, but also file_get_contents cuts the response down. At various points.
cURL does the same, cuts down the response json at various points. Looks "at random"
Original post:
I'm breaking my head on a strange issue here. I've created a PHP Wrapper for Wunderlist2 (http://www.wunderlist.com) which can be found here: https://github.com/PENDOnl/Wunderlist2-PHP-Wrapper
It worked perfectly until I was notified by a user that the class stopped working all of a sudden. Since I created a free service (http://wcal.me) to provide a calendar feed for Wunderlist users, I decided to take a look at that script since that's the most easy way for me to debug the script.
Login in to Wunderlist and fetching the authtoken works, also all other functions in the class seem to work find (getting 'me', 'me/lists', etc.) However, in case of the getTasks function ('me/tasks') the response that I get is not complete, it simply stops in the middle of a json string. Therefor the json_decode function returns NULL and thus no tasks will be available in the calender feed/method response.
I also noticed that it's pretty random, because in some cases (<10%) it works like it should, but after another refresh the output is cut in half again. Also, the exact location of the 'cut' is different per refresh.
Is there anyone that can identify what the problem is? I tried to see if there is a way to wait untill a complete download of the file before it is returned, but it would be strange if that wasn't the default behaviour of cURL. I also tried to increase the timeout time, but since it returns a value I guess it doesn't timeout either.
All code can be foundin the Github repo, so far this is the only part I've changed to debug:
// get / put / delete requests should have HTTP Code 200 OK
// only exception is the login method, which returns HTTP Code 200 OK
if($httpCode == 200 && (strtolower($method) != 'post' || $action == '/login'))
{
$return = json_decode($output, true);
if($_SERVER['REMOTE_ADDR'] == MY_IP) {
if( is_null($return) ) {
echo "<b>Output of json_decode is null:</b><br><br>";
echo $output;
} else {
echo "<b>Output of json_decode os not null:</b><br><br>";
echo $output;
}
}
return $return;
}
The full response should look like something similar to this:
[{"assignee_id":null,"completed_at":null,"completed_by_id":null,"created_at":"2013-11-10T13:16:54Z"},{"assignee_id":null,"completed_at":null,"completed_by_id":null,"created_at":"2013-11-10T13:16:54Z"}]
But instead in most cases it's:
[{"assignee_id":null,"completed_at":null,"completed_by_id":null,"created_at":"2013-11-10T13:16:54Z"},{"assignee
Of course, this is a minified response, there is more information in the response available and there are many more items in the array.
For testing purposes, if you authenticate with the class, what result does browsing to http://api.wunderlist.com/me/tasks yield?
Also I'm sure you've already fixed but in base.class.php, $action is being checked for empty values twice instead of $action & $method.
Problem was with Wunderlist, just waiting for their fix :)

No element found FF - Error visible in firebug

I'm creating a global high score table. The JavaScript sends the username and score to the PHP through some AJAX functions and POST. The PHP script then takes the variable, and compares the score values to the file on the server. It then puts the new score where it should go on the score table, and rewrites the file.
I had it working last night, but now, when I call the function, it displays my code instead, and firebug gives the error, "No element found". I'm not sure what I did between then and now, but I just can't figure this one out.
This is my first time using PHP and AJAX, so I'm not sure where or what the error is.
Any help would be appreciated!
The exact error:
Timestamp: 4/13/2013 1:59:18 PM
Error: no element found
Source File: file:///C:/wamp/www/ajax.php
Line: 84, Column: 3
Source Code:
?>
If I cannot help you solve this issue, I will delete this answer, but I wanted to post it like this so you can better read my suggestions.
Start by changing your HandleResponse function to this:
function HandleResponse(response)
{
console.log(response);
document.getElementById('ResponseDiv').innerHTML = response;
}
and post the result that appears above the error, in firebug.
What we are doing here is adding a way for us to see if any data was actually returned from the AJAX request. A call to console.log tells the browser to print the given argument to the console (in your case, firebug).
UPDATE 1
In your comments, you stated that nothing displayed when you logged the value of response. This means that you received nothing back from the server (in terms of data, at least).
My next suggestion is that you change the call to MakeRequest to the following:
<input type='button' id="test" onclick='MakeRequest("*");' value='Global Highscore Table'/>
Like before, leave the console.log line in the HandleResponse method, and post the result that appears above the error in firebug.
Here, we are setting your MakeRequest to make a request that passes a wild-card query parameter. At the time of this suggestion, I did not realize that the requested file expected two arguments, nor that passing a wild-card would error the script. However, this was still a good thing to do, as we found a new error, which tells us that the request is being received.
UPDATE 2
Now, cut and paste everything from your "ajax.php" file to your notepad or another, similar application. Then, set the following to be the only content of the "ajax.php" file:
<?php echo "Request received and response sent"; ?>
Again, post the result of the console.log.
Here, we are taking a step back from fixing the complex code, and we are going back to the basics. All we want to do, here, is verify that we can both send the request and receive a response.
If "Request received and response sent" is successfully returned as the response of the request, then we know that the error lies in ajax.php, and not in the request. If it is not, we know that there is a problem with the request (be it a server issue or the request, itself). If the latter is the case, there could still be a problem with ajax.php, but we must first fix the request.
UPDATE 3 (final solution)
Issue was found to be due to not correctly running on the local server.

PHP - Error 500 after page refresh

I've got a PHP application and I'm having trouble getting a User object from the SESSION.
When saving the object I have the following code (some other file).
$_SESSION['user'] = serialize($user);
The problem begins when I try and load a file called timeline.php. The first time I load the page (after I've logged in and the SESSION['user] is set) the page works fine. When I refresh the page I get an error 500.
The error log shows this:
Call to a member function get() on a non-object in timeline.php on line 10.
The code from line 2-10 is this:
include_once('isUserLoggedIn.php');
require_once('classes/User.php');
session_start();
$user = unserialize($_SESSION['user']);
require_once('classes/Database.php');
$tweets = Database::getTimeline($user->get('user_id'));
Can anyone offer any reason for this?
UPDATE (1)
In timeline.php I added var_dump($user) to line 8. The first time I load the page it gives me the correct value.
The second time I load the page it gives me bool(false).
UPDATE (2)
The problem seems to be with the host (perhaps a setting in the php.ini file there). The company I was using is Fatcow. When I used someone elses host (1and1) the issue didn't happen.
When unserializing objects lots of strange things can occur, see the PHP manual page for more information.
http://php.net/manual/en/function.unserialize.php
This comment seems pertinent:
http://www.php.net/manual/en/function.unserialize.php#77826

Magento Fatal Error: Call to member function getSku() on a non-object

I have come across a bizarre error in the Magento shop I'm developing and despite my inquiries online, it appears no one else has ever seen this exact error under the same circumstances. Lemme 'splain.
The full text of the error message is this:
Fatal error: Call to a member function getSku() on a non-object in /path/on/server/app/code/core/Mage/Catalog/Model/Product/Option/Type/Select.php on line 221.
Now, others have gotten this error message--it was addressed and supposedly fixed in the 1.3.1 roadmap (http://www.magentocommerce.com/roadmap/release/1.3.1). However, the circumstances of those other error messages were where they tried to add an item to the cart--if the item had custom options, it would loop to this error message.
My situation is that I have a SIMPLE item--not bundled or configurable--without any custom options. I can add it to the cart without any trouble. But if I run through the entire checkout procedure, upon placing the order, the error message appears on a white screen. The URL in the browser shows me I’m on the checkout success page.
AND, the order appears to go through perfectly, getting registered by both Magento AND Authorize.net.
I’ve tried debugging the error as far as I can go, but this one’s got me stumped.
For reference, I’m in Magento 1.3.2.4. When I first received the error I reinstalled all the core files and was still able to replicate the error.
I’m going to continue to test, but if anyone has ANY bright ideas about why this is happening, I’d love to hear your thoughts. I’m so close to launch and this thing could put the kibosh on the whole thing.
I've had this error before and was also not getting any hits on how to solve it. So, I had to modify the core files to fix the error message. The problem is Mage_Catalog_Model_Product_Option::getValueById() can return a null value BUT Catalog/Model/Product/Option/Type/Select.php is NOT checking for this possibility.
Here's the solution that worked for me:
Open app/code/core/Mage/Catalog/Model/Product/Option/Type/Select.php
Change line 121 from:
$result = $option->getValueById($optionValue)->getSku();
To:
$o= $option->getValueById($optionValue);
$result = is_object($o) ? $o->getSku() : null;
I always hate changing core files but when there's a bug there's not much else I can do!
I followed that steps that pygorex1 posted but also replaced lines 191-195 in the Select.php file, because I was still getting another similar error about the getPrice(). I have Magento ver. 1.3.2.4.
Original code from lines 191-195:
$result = $this->_getChargableOptionPrice(
$option->getValueById($optionValue)->getPrice(),
$option->getValueById($optionValue)->getPriceType() == 'percent',
$basePrice
);
Here is the code I created to replace lines 191-195:
$z= $option->getValueById($optionValue);
$result = is_object($z) ? $z ->getPrice() : null;
$zz = $option->getValueById($optionValue);
$result = is_object($zz) ? $zz ->getPriceType() == 'percent' : $basePrice;
FYI - I am NOT a PHP programmer. I just happened to figure out how to rework the code to make fix this problem based on pygorex1's code. So, there is a good chance I didn't write it correctly. However, it did fix the problem for me (which I am rather proud of :)
Got same problem. Backtraced it and identified it happens for me when editing orders in admin and that order contains at least one product with an individual option that was removed since the order has been placed.
Fixed three files then so that product is simply removed when editing such an order - all modifications working in "local" scope so core files left untouched:
1. app/code/local/Mage/Catalog/Model/Product/Option/Type/Select.php:221
(search)
$result = $option->getValueById($optionValue)->getSku();
(prepend)
/* hotfix for - PHP Fatal error: Call to a member function getSku() on a non-object - occurs on admin order edit if a product option has been removed */
if (is_null($option->getValueById($optionValue))) {
throw new Exception('missing product option');
}
2. app/code/local/Mage/Sales/Model/Quote.php:695
(search)
$item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());
(replace)
/* hotfix for - PHP Fatal error: Call to a member function getSku() on a non-object - occurs on admin order edit if a product option has been removed */
try {
$item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());
} catch ( Exception $e ) {
if ($e->getMessage()=='missing product option') { return null; }
throw new Exception($e->getMessage());
}
3. app/code/local/Mage/Adminhtml/Model/Sales/Order/Create.php:288
(search)
if (is_string($item)) {
return $item;
}
(replace)
if (is_string($item)) {
return $item;
/* hotfix for - PHP Fatal error: Call to a member function getSku() on a non-object - occurs on admin order edit if a product option has been removed */
} elseif (is_null($item)) {
return $this;
}
Well, I had the same error for my client, Magento was working fine on one server but not on the other so I though it must be something wrong with new installation (after migration). I did not play with the code, just repaired the DB and it started to work properly.

Categories