Laravel 5 Parse - php

I have the following problem i'm using laravel 5 and laraparse package.Login with parse works without a problem,also things like insert categories works.The problem is for sign up i'm using ParseUser().I use the following code from parse docs for sign up:
$user = new ParseUser();
$user->set("username", $request->username);
$user->set("email", $request->email);
$user->set('isArtist', $isArtist);
$user->set("password", $request->password);
try {
$user->signUp();
return redirect('profile');
} catch (ParseException $ex) {
// Show the error message somewhere and let the user try again.
echo "Error: " . $ex->getCode() . " " . $ex->getMessage();
}
but it returns the following error:
You must specify a Parse class name or register the appropriate subclass when creating a new Object. Use ParseObject::create to create a subclass object.
The keys in config are ok because login and everything else works so the keys are not the problem.

Related

Parse PHP current user update failed but session updated

In Parse PHP SDK If the current user want to change his Email/username normally Parse check if username used by other user and if it is used it will return error, now this is good and work perfectly so far but the issue is the session is automatically updates to the new value where it failed.
so basically the session for the current user updated even if it wasn't for the backend
Steps to reproduce
$currentUser = Parse\ParseUser::getCurrentUser();
echo "Current Username is : ". $currentUser->get("username");
if ($currentUser) {
$currentUser->set("username", "ww");
try {
$currentUser->save();
echo "UPDATED";
} catch (Parse\ParseException $er) {
$ex = $er->getMessage();
echo "<br> Error: ". $ex;
}
}
here is a video that explains more:
https://youtu.be/KWS9fW5MReA
Since you have updated the object in your PHP application, it will keep updated locally unless you reset the action. So you can either:
save the old username and reverse the action in your catch method; or
use $currentUser->fetch() in your catch method; or
instantiate a new user object, do the change attempt in this new object, and finally $currentUser->fetch() only in case of success.
See below one of the possible solutions:
$currentUser = Parse\ParseUser::getCurrentUser();
echo "Current Username is : ". $currentUser->get("username");
if ($currentUser) {
$currentUser->set("username", "ww");
try {
$currentUser->save();
echo "UPDATED";
} catch (Parse\ParseException $er) {
$currentUser->fetch();
$ex = $er->getMessage();
echo "<br> Error: ". $ex;
}
}

Facebook SDK not catching exceptions

I'm using the Facebook Graph API SDK in my Laravel 5.4 app ("facebook/graph-sdk": "~5.0" in composer)
I'm trying to fetch some fields from a page and when the page is invalid it throws an error, however it doesn't seem to catch the error properly:
Code below:
use Facebook\Facebook;
class FacebookUser extends Controller
{
try {
echo 'Trying ' . $venue->id;
$response = $fb->get('/'.$page.'/locations?fields=hours', $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
This isn't ideal as it stops every time it hits a snag with the following error:
In FacebookResponseException.php line 106:
(#100) Tried accessing nonexisting field (hours) on node type (URL)
The exception is most likely named Facebook\Exceptions\FacebookResponseException.
Because you have:
use Facebook\Facebook;
which is:
use Facebook\Facebook as Facebook;
you are ending up trying to catch this class:
Facebook\Facebook\Exceptions\FacebookResponseException
You probably want to adjust your catch like so:
} catch (\Facebook\Exceptions\FacebookResponseException $e) {

catching mailchimp php api errors

I am trying to create a subscribe method for my laravel app that uses the mailchimp api to subscribe a user to a given list. The method works fine when the email address is not already on the lsit. when it is already subscribed the mailchimp api throws the following error
Mailchimp_List_AlreadySubscribed blah#blah.co is already subscribed to
list Tc App Test List. Click here to update your profile.
with the following code being shown
public function castError($result) {
if($result['status'] !== 'error' || !$result['name']) throw new Mailchimp_Error('We received an unexpected error: ' . json_encode($result));
$class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mailchimp_Error';
return new $class($result['error'], $result['code']);
}
I have attempted a try catch block to catch the error but it is still being returned to the browser, here is what I tried and were it says MailChimp_Error I tried with Exception as well.
public function subscribe($id, $email, $merge_vars)
{
try {
$this->mailchimp->lists->subscribe($id, $email, $merge_vars);
} catch (MailChimp_Error $e) {
$response = 'an error has occured';
}
return $response;
}
Ultimately I want to be able to run the method and then either return either a success message or a message describing the issue to the user. the 3 possible mailchimp method errors are Email_notexists, list_alreadysubscribed and list does not exist although tihs last one should not occur as I am providing the list in the source code.
edit 1; after being in touch with mailchimp api support they suggested this code but the error still gets returned to the browser in its entirety
try {
$results = $this->mailchimp->lists->subscribe($id, $email, $merge_vars);
} catch (Mailchimp_Error $e) {
if ($e->getMessage()) {
$error = 'Code:'.$e->getCode().': '.$e->getMessage();
}
}
echo $error;
You can do
try
{
$response = $this->mailchimp->lists->addListMember($list_id, [
"email_address" => $email,
"status" => "subscribed",
]);
}
catch (\EXCEPTION $e) {
return $e->getMessage();
}
The \EXCEPTION handles a sort of error for stripe
Subscribe is in a namespace Acme\Emails\Subscribe so catch(Mailchimp_Error $e) looks for Mailchimp_Error in this namespace.
Changing it to catch(\Mailchimp_Error $e) makes it look in the root namespace and then it works as intended

How to validate Facebook App ID

I need to check if the given Facebook app id is valid. Also, I need to check which domain and site configurations are set for this app id. It doesn't matter if it's done through PHP or Javascript.
I checked everywhere but couldn't find any information about this. Any ideas?
You can validate the ID by going to http://graph.facebook.com/<APP_ID> and seeing if it loads what you expect. For the app information, try using admin.getAppProperties, using properties from this list.
Use the Graph API. Simply request:
https://graph.facebook.com/<appid>
It should return you a JSON object that looks like this:
{
id: "<appid>",
name: "<appname>",
category: "<app category>",
subcategory: "<app subcategory>",
link: "<applink>",
type: "application",
}
So, to validate if the specified app_id is indeed the id of an application, look for the type property and check if it says application. If the id is not found at all, it will just return false.
More info: https://developers.facebook.com/docs/reference/api/application/
For example:
<?php
$app_id = 246554168145;
$object = json_decode(file_get_contents('https://graph.facebook.com/'.$app_id));
// the object is supposed to have a type property (according to the FB docs)
// but doesn't, so checking on the link as well. If that gets fixed
// then check on isset($object->type) && $object->type == 'application'
if ($object && isset($object->link) && strstr($object->link, 'http://www.facebook.com/apps/application.php')) {
print "The name of this app is: {$object->name}";
} else {
throw new InvalidArgumentException('This is not the id of an application');
}
?>
Use the Graph API:
$fb = new Facebook\Facebook(/* . . . */);
// Send the request to Graph
try {
$response = $fb->get('/me');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
var_dump($response);
// class Facebook\FacebookResponse . . .
More info:FacebookResponse for the Facebook SDK for PHP

How to get a message in a variable if the page contains an error?

While we run an web application some page may contain error and some page maynot contain error,
I want to get a notification if the page contains some error ,If there is an error we can see the error in the page, but can we set any value to a variable if the page contains error.. such that we can get the notification that there is an error .
I want to get the notification since i want to create an error log,If we can set the variable with some value then we can use some condition to create a logfile
How can we do that?
There is several ways to do it. One is to setup a custom error handler. PHP will trap most errors raised during script execution and pass it to your custom handler then. What you do inside the handler is up to you. You can write to a log and then redirect to somewhere else or whatever you want.
If you are talking about Exceptions, then wrap code that can break in try/catch blocks. If an error occurs, handle the exception the catch block. What you put in there is again up to you.
Go through the linked pages to learn how this works. Catching an error, setting a variable and writing to a log are three distinct things. Isolate and solve them one by one.
You could also consider using a try { } catch { } block and writing exceptions to error log in catch { } part. Like this:
try {
$db = new MyDb('127.0.0.1', 'root', 'root');
if (false === $db) {
throw new Exception ('Could not connect to the database.');
}
$row = $db->getTable('table_name')->getRowByColumn('id', $_GET['id']);
if (null === $row) {
throw new Exception ('Row with id ' . $_GET['id'] . ' not found.')
}
// and so on
} catch (Exception $e) {
$fp = fopen('logs/error.txt', 'w');
fwrite($fp, date('l jS \of F Y h:i:s A') . ': ' . $e->getMessage() . "\n");
fclose($fp);
}
You get the idea.
Instead of just a date of error you could also append a login of signed in user if the script is in authentication protected area so you know which user got that error.

Categories