I have video rooms (via Twig) being created on my page when the users want to start a video chat between each other. When they leave the room, the room is being deleted after a period of time so when I try to access it, it drops an error ({room_id} does not exist}. Below is the function:
/**
* #Route("/video/join/{room_name}", name="videochat_join")
*
* #param $room_name
*
* #return RedirectResponse|Response
*
* #throws \Twilio\Exceptions\ConfigurationException
* #throws \Twilio\Exceptions\TwilioException
*/
public function joinVideo($room_name)
{
$user = $this->getCurrentUser();
$twilio = new Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'));
$room = $twilio->video->v1->rooms($room_name)->fetch();
$roomSid = $room->sid;
$token = new AccessToken(getenv('TWILIO_ACCOUNT_SID'), getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), 3600, $user->getEmail());
$videoGrant = new VideoGrant();
$videoGrant->setRoom($room_name);
$token->addGrant($videoGrant);
return $this->render('chat/video_join.html.twig', [
'roomSid' => $roomSid,
'roomName' => $room_name,
'accessToken' => $token->toJWT(),
]);
};
How can I catch if the room is no longer available and forward user to 404_room.html.twig? Because it doesn't redirect to the default 404 template.
The error is:
RestException
Twilio\Exceptions\RestException:
[HTTP 404] Unable to fetch record: The requested resource /Rooms/1_2room808823 was not found
at vendor/twilio/sdk/Twilio/Version.php:85
at Twilio\Version->exception(object(Response), 'Unable to fetch record')
(vendor/twilio/sdk/Twilio/Version.php:109)
at Twilio\Version->fetch('GET', '/Rooms/1_2room808823', array())
(vendor/twilio/sdk/Twilio/Rest/Video/V1/RoomContext.php:58)
at Twilio\Rest\Video\V1\RoomContext->fetch()
(src/Controller/Chat/VideoController.php:93)
at App\Controller\Chat\VideoController->joinVideo('1_2room808823')
(vendor/symfony/http-kernel/HttpKernel.php:149)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:66)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:188)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:37)
I have tried to do:
try{
($twilio->video->v1->rooms($room_name)->fetch());
echo "Room exists"; //this one is working fine
} catch ( TwilioException $e ) {
echo 'Caught exception: ', $e->getMessage(), "\n"; //this doesn't
}
...without luck
Add the following use statements to the top of your Controller class.
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twilio\Exceptions\RestException;
Then wrap your code in a try/catch block. If you catch the expected RestException, then you can throw a NotFoundHttpException to force the 404 response. For example:
/**
* #Route("/video/join/{room_name}", name="videochat_join")
*
* #param $room_name
*
* #return RedirectResponse|Response
*
* #throws \Twilio\Exceptions\ConfigurationException
* #throws \Twilio\Exceptions\TwilioException
*/
public function joinVideo($room_name)
{
try {
$user = $this->getCurrentUser();
$twilio = new Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'));
$room = $twilio->video->v1->rooms($room_name)->fetch();
$roomSid = $room->sid;
$token = new AccessToken(getenv('TWILIO_ACCOUNT_SID'), getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), 3600, $user->getEmail());
$videoGrant = new VideoGrant();
$videoGrant->setRoom($room_name);
$token->addGrant($videoGrant);
return $this->render('chat/video_join.html.twig', [
'roomSid' => $roomSid,
'roomName' => $room_name,
'accessToken' => $token->toJWT(),
]);
}
catch (RestException $exception) {
throw new NotFoundHttpException("'{$room_name}' could not be found");
}
}
Related
We have created a REST API PHP setup which is designed to compare the Tables of one database with another and if the user doesn't exist in one, create it in the Application. Also if there is a change to a user i.e. Email address, string etc then update it.
We've had a script created which using php works fantastically on our test server, it creates a user when required and updates them when required.
When we move this over to a different server which has the same database structure, same tables, same root username and password etc, it isn't recognising the existing users. It thinks it has xx,xxx users to create and then fails as 'user with that username already exists'.
Does any one have any ideas why this maybe the case? Below is the main php file which then has a config file, a db.php, a common.php and then a GpsGateapi.php
require_once('rest_api_includes/Config.php');
require_once(Config::API_INCLUDES_DIR . '/DB.php');
require_once(Config::API_INCLUDES_DIR . '/Gpsgate_API.php');
require_once(Config::API_INCLUDES_DIR . '/Common.php');
class API_Sync
{
/** #var int application id */
private $application_id;
/** #var int user type id */
private $user_type_id;
/** #var Gpsgate_API instance */
private $api;
/** #var DB instance */
private $db;
/** #var string<show|log> Show or log errors */
private $errors_report_type;
/** #var string<show|log|none> Show or log actions */
private $actions_report_type;
/**
* Sets variables, connects to database and REST API
*
* #throws \Exception
*/
public function __construct()
{
$this->application_id = Config::API_APPLIATION_ID;
$this->user_type_id = Config::API_USER_TYPE_ID;
$this->errors_report_type = Config::API_SHOW_OUTPUT ? 'show' : 'log';
$this->actions_report_type = Config::API_SHOW_OUTPUT ? 'show' : (Config::API_ACTIONS_LOGGING ? 'log' : 'none');
}
/**
* Main method that controlls all the process
*
* #throws \Exception
*/
public function run()
{
$this->prepare();
$gpsgate_users_result = $this->api->getUsers();
if ($gpsgate_users_result->status !== 200) {
throw new Exception(implode('; ', $gpsgate_users_result->body));
}
$gpsgate_users = $gpsgate_users_result->body;
$db_users = $this->db->get(DB::TBL_PERSONS);
$res = $this->compare($gpsgate_users, $db_users);
$this->updateUsers($res['update']);
$this->createUsers($res['create']);
}
/**
* Create gsgate users data from database array
*
* #param array $users
*/
private function createUsers($users)
{
if (!empty($users)) {
$this->logOrShowAction("Begin to create users data.");
foreach ($users as $user) {
$res = $this->api->createUser([
'email' => $user['Email'],
'name' => $user['FirstName'],
'surname' => $user['LastName'],
'driverID' => $user['RFID'],
'username' => $user['UserName'],
'password' => Common::generatePassword(),
'userTypeId' => $this->user_type_id,
'description' => $user['Location'],
]);
if ($res->status !== 200) {
throw new Exception($res->body);
}
}
$this->logOrShowAction("Done.");
}
}
/**
* Update gsgate users data from database array
*
* #param array $users
*/
private function updateUsers($users)
{
if (!empty($users)) {
$this->logOrShowAction("Begin to update users data.");
foreach ($users as $user) {
$res = $this->api->updateUser([
'email' => $user['Email'],
'name' => $user['FirstName'],
'surname' => $user['LastName'],
'driverID' => $user['RFID'],
'description' => $user['Location'],
], $user['gpsgate_id']);
if ($res->status !== 200) {
throw new Exception($res->body);
}
}
$this->logOrShowAction("Done.");
}
}
/**
* Compare arrays and return list of users data to update/create
*/
private function compare($gpsgate_users, $db_users)
{
$this->logOrShowAction("Begin to compare users data.");
$gpsgate_user_key = 'username';
$db_user_key = 'UserName';
$gpsgate_users = Common::setIndexesByKey($gpsgate_user_key, $gpsgate_users);
$res = [
'update' => [],
'create' => [],
];
foreach ($db_users as $user) {
$user_key = $user[$db_user_key];
if (!empty($gpsgate_users[$user_key])) {
if ($this->userInfoVary($gpsgate_users[$user_key], $user)) {
$user['gpsgate_id'] = $gpsgate_users[$user_key]->id; // add gpsgate id
$res['update'][] = $user;
}
} else {
$res['create'][] = $user;
}
}
$this->logOrShowAction('Done');
$this->logOrShowAction('Need to create: ' . count($res['create']) . ' rows; to update: ' . count($res['update']) . ' rows.');
return $res;
}
/**
* Check is the information between db user and api user is different
*/
private function userInfoVary($gpsgate_user, $db_user)
{
return $db_user['Email'] != $gpsgate_user->email
|| $db_user['FirstName'] != $gpsgate_user->name
|| $db_user['LastName'] != $gpsgate_user->surname
|| $db_user['RFID'] != ($gpsgate_user->driverID ?? '')
|| $db_user['Location'] != $gpsgate_user->description;
}
/**
* Connects to database, REST API, creates folders for errors and actions if need
*/
private function prepare()
{
if ($this->errors_report_type == 'log') {
if (!file_exists(dirname(Config::API_LOG_FILE_ERRORS))) {
mkdir(dirname(Config::API_LOG_FILE_ERRORS));
}
if (!file_exists(Config::API_LOG_FILE_ERRORS)) {
file_put_contents(Config::API_LOG_FILE_ERRORS, '');
}
}
if ($this->actions_report_type == 'log') {
if (!file_exists(dirname(Config::API_LOG_FILE_ACTIONS))) {
mkdir(dirname(Config::API_LOG_FILE_ACTIONS));
}
if (!file_exists(Config::API_LOG_FILE_ACTIONS)) {
file_put_contents(Config::API_LOG_FILE_ACTIONS, '');
}
}
$this->logOrShowAction('Trying to connect to database and GPSGATE REST API.');
$this->api = new Gpsgate_API(Config::API_APPLIATION_ID, Config::API_USER_TYPE_ID);
$this->db = DB::instance();
$this->logOrShowAction('Done.');
}
/**
* Logs error message in file or output in browser
*
* #param string $msg
*/
public function logOrShowError($msg)
{
$msg = "<span style='color: red; font-weight: 600;'>Error: " . $msg . "</span>";
$this->writeOrEchoMessage($msg, Config::API_LOG_FILE_ERRORS, $this->errors_report_type);
}
/**
* Logs action message in file or output in browser
*
* #param string $msg
*/
public function logOrShowAction($msg)
{
$this->writeOrEchoMessage($msg, Config::API_LOG_FILE_ACTIONS, $this->actions_report_type);
}
private function writeOrEchoMessage($msg, $file, $report_type)
{
if ($report_type == 'none') {
return ;
}
$msg = '[' . date('Y-m-d H:i:s') . '] ' . $msg;
if ($report_type == 'show') {
echo $msg . '<br>';
} else {
$h = fopen($file, 'a+');
fwrite($h, strip_tags($msg) . PHP_EOL);
fclose($h);
}
}
}
$sync = new API_Sync();
try {
$sync->run();
} catch (\Exception $e) {
$sync->logOrShowError($e->getMessage());
}
I am getting a encoding error with eloquent and i don't understand how to fix it
this is the the error:
Illuminate\Database\Eloquent\JsonEncodingException: Error encoding model [App\Models\UserIp] with ID [] to JSON: Recursion detected in file
this is the the function :
/**
* #param string $ip the user ipaddress
* #param string $status failed or succeeded ?
* #param string $type the type of action performed
*
* #throws \JsonException
*/
function logUserIp(string $ip, string $status, string $type)
{
$user = UserIp::where('ip', '=', $ip)->where('user_id', '=', auth()->user()->id)->first();
$position = Location::get($ip);
if (null == $user && false !== $position) {
$ip = new UserIp();
$ip->user_id = auth()->user()->id;
$ip->ip = $ip;
$ip->country = $position->countryName;
$ip->country_code = $position->countryCode;
$ip->status = $status;
$ip->type = $type;
$ip->device = json_encode([
'platform' => Agent::platform(),
'browser' => Agent::browser(),
'user_agent' => \request()->server('HTTP_USER_AGENT')
], JSON_THROW_ON_ERROR);
$ip->save();
}
}
There error is occuring from this line:
$ip->ip = $ip;
You have a parameter named $ip in your function signature (string $ip) but you're then overriding that value later with $ip = new UserIp(), so $ip is no longer a string but is now an instance of UserIp.
Later in your code you then assign the new $ip of type UserIp to your $ip->ip property. You're basically assigning the $ip instance of UserIp to the ip property on itself. I assume you actually mean to be the original string $ip.
Change the name of your new UserIp() variable and you should be good.
I saw you fixed your problem, but let me give you some small tips about your code, see my code and compare it with yours:
/**
* #param string $ip the user ipaddress
* #param string $status failed or succeeded ?
* #param string $type the type of action performed
*
* #throws \JsonException
*/
public function logUserIp(string $ip, string $status, string $type): void
{
$user = auth()->user();
if ($position = Location::get($ip) &&
!$user->userIp()->where('ip', $ip)->exists()
) {
$userIp = new UserIp();
$userIp->ip = $ip;
$userIp->country = $position->countryName;
$userIp->country_code = $position->countryCode;
$userIp->status = $status;
$userIp->type = $type;
$userIp->device = [
'platform' => Agent::platform(),
'browser' => Agent::browser(),
'user_agent' => request()->server('HTTP_USER_AGENT')
];
$user->associate($userIp);
$user->save();
}
}
See how I changed the code. Also, for $userIp->device to save without the need of json_encode, you will have to add this to your UserIp model:
/**
* The attributes that should be cast.
*
* #var array
*/
protected $casts = [
'device' => 'array',
];
So you can later consume $userIp->device as an array, no need to do json_decode($userIp->device).
Also, related to $user->associate, read this.
I have this controller that is supposed to perform PayPal payments. The payment function is working well but on getting to success function I am getting an error Illegal string offset 'total' . I am passing $this->productData($request) as suggested in this question. I tried creating a variable $total = $response['AMT'] which is the response from setCheckoutDetails but I still got the same error. How do I go about it?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\ExpressCheckout;
class PayPalController extends Controller
{
private function projectData(Request $request){
// dd($request->all());
$item = [];
$datat = array_map(function($item){
return [
'name'=>$request->project_id,
'price'=>$request->budget,
'desc'=>'Deposit',
'qty'=>1
];
}, $item);
$data = [
'items'=>$datat,
'invoice_id' => uniqid(),
'invoice_description' => "Payment for Project No.".$request->project_id." Amount ".$request->budget,
'return_url' => route('payment.success'),
'cancel_url' => route('payment.cancel'),
'total'=>$request->budget
];
// dd($data);
return $data;
}
/**
* Responds with a welcome message with instructions
*
* #return \Illuminate\Http\Response
*/
public function payment(Request $request) {
$data = $this->projectData($request);
$provider = new ExpressCheckout;
$response = $provider->setExpressCheckout($data);
// dd($response);
// $response = $provider->setExpressCheckout($data, true);
return redirect($response['paypal_link']);
}
/**
* Responds with a welcome message with instructions
*
* #return \Illuminate\Http\Response
*/
public function cancel()
{
dd('Your payment is canceled. You can create cancel page here.');
}
/**
* Responds with a welcome message with instructions
*
* #return \Illuminate\Http\Response
*/
public function success(Request $request)
{
$provider = new ExpressCheckout;
$response = $provider->getExpressCheckoutDetails($request->token);
$token = $response['TOKEN'];
$payerId = $response['PAYERID'];
$total = $response['AMT'];
// dd($response);
if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
// dd('Payment successful');
//Performing transaction
$payment_status = $provider->doExpressCheckoutPayment($token, $payerId, $this->projectData($request));
dd($payment_status);
}
dd('Something is wrong.');
}
}
You have to pass three parameters
data, token, PAYERID
Data can service information like
$data = array(
'total' => Total amount,
'invoice_id' => Invoicen number,
'invoice_description' => invoice descrption
);
And items as well which will contain name, price, desc and qty
The action included at the end of this post is called from an ajax call via jQuery. When I try to delete more than one record, just the first is deleted but the flash messages are printed for every request.
I checked the input data and it is correct. No exception are throwed.
Any idea? Thanks!
/**
* #Route("/cancella", name="training_plan_activity_edition_final_delete")
* #Method({"POST"})
*
* #param Request $request
* #return \Symfony\Component\HttpFoundation\Response
*/
public function deleteAction(Request $request)
{
if (!$request->isXmlHttpRequest()) {
throw new \Exception('this controller allows only ajax requests');
}
$ids = json_decode($request->get('ids'), true);
$em = $this->getDoctrine()->getManager();
foreach ($ids as $id) {
if($id <> 'on') {
$trainingPlanActionEditionFinal = $this->getDoctrine()
->getRepository('TrainingPlanBundle:TrainingPlanActionEditionFinal')
->find($id);
if (!$trainingPlanActionEditionFinal) {
throw $this->createNotFoundException(
'Training plan action edition final with id ' . $id . ' not found'
);
}
$trainingPlanSlug = $trainingPlanActionEditionFinal->getSlug();
try {
$em->remove($trainingPlanActionEditionFinal);
$em->flush();
$this->get('session')->getFlashbag()->add('success', 'Cancellazione dell\'edizione avvenuta con successo');
} catch (\Exception $e) {
$this->get('session')->getFlashbag()->add('error', 'Impossibile cancellare l\'edizione');
}
}
}
return new Response($this->generateUrl('training_plan_action_edition_final_list', array(
'trainingPlanSlug' => $trainingPlanSlug
)
));
}
I'm trying to implement GCM server using PHP and Zend Framework on Google App Engine. So far it works fine locally, but fails with this message when uploaded to App Engine:
Here is the code:
$ids = '....my device id....';
$apiKey = '...my api key...';
$data = array( 'message' => 'Hello World!' );
$gcm_client = new Client();
$gcm_client->setApiKey($apiKey);
$gcm_message = new Message();
$gcm_message->setTimeToLive(86400);
$gcm_message->setData($data);
$gcm_message->setRegistrationIds($ids);
$response = $gcm_client->send($gcm_message);
var_dump($response);
And it fails with this error message:
PHP Fatal error: Uncaught exception 'ErrorException' with message
'stream_socket_client(): unable to connect to
android.googleapis.com:443 (Unknown error 4294967295)' in
/base/data/home/..../backend:v1.375711862873219029/vendor/zendframework/zend-http/Zend/Http/Client/Adapter/Socket.php:253
I know App Engine doesn't allow socket connections and offers urlFetch wrapper for http and https, but how do I tell Zend Framework to use this transport?
Try enabling Billing. As far as I remember sockets are enabled only for paid apps.
This won't charge you anything (unless you exceed free quota) but should get rid of the error.
Promoted this from a comment - I ended up making my own class implementing Zend\Http\Client\Adapter\AdapterInterface that uses URLFetch by opening a URL using the usual fopen with stream context to send POST request. Although this works I'm not sure it's the best way. Would prefer to use the framework capabilities, if possible.
I'm not sure if this is going to help anyone, as both ZendFramework and AppEngine have evolved since I asked the question, but here is the adapter I've implemented:
use Zend\Http\Client\Adapter\AdapterInterface;
use Zend\Http\Client\Adapter\Exception\RuntimeException;
use Zend\Http\Client\Adapter\Exception\TimeoutException;
use Zend\Stdlib\ErrorHandler;
class URLFetchHttpAdapter implements AdapterInterface
{
protected $stream;
protected $options;
/**
* Set the configuration array for the adapter
*
* #param array $options
*/
public function setOptions($options = array())
{
$this->options = $options;
}
/**
* Connect to the remote server
*
* #param string $host
* #param int $port
* #param bool $secure
*/
public function connect($host, $port = 80, $secure = false)
{
// no connection yet - it's performed in "write" method
}
/**
* Send request to the remote server
*
* #param string $method
* #param \Zend\Uri\Uri $url
* #param string $httpVer
* #param array $headers
* #param string $body
*
* #throws \Zend\Loader\Exception\RuntimeException
* #return string Request as text
*/
public function write($method, $url, $httpVer = '1.1', $headers = array(), $body = '')
{
$headers_str = '';
foreach ($headers as $k => $v) {
if (is_string($k))
$v = ucfirst($k) . ": $v";
$headers_str .= "$v\r\n";
}
if (!is_array($this->options))
$this->options = array();
$context_arr = array("http" =>
array( "method" => $method,
"content" => $body,
"header" => $headers_str,
"protocol_version" => $httpVer,
'ignore_errors' => true,
'follow_location' => false,
) + $this->options
);
$context = stream_context_create($context_arr);
ErrorHandler::start();
$this->stream = fopen((string)$url, 'r', null, $context);
$error = ErrorHandler::stop();
if (!$this->stream) {
throw new \Zend\Loader\Exception\RuntimeException('', 0, $error);
}
}
/**
* Read response from server
*
* #throws \Zend\Http\Client\Adapter\Exception\RuntimeException
* #return string
*/
public function read()
{
if ($this->stream) {
ErrorHandler::start();
$metadata = stream_get_meta_data($this->stream);
$headers = join("\r\n", $metadata['wrapper_data']);
$contents = stream_get_contents($this->stream);
$error = ErrorHandler::stop();
if ($error)
throw $error;
$this->close();
//echo $headers."\r\n\r\n".$contents;
return $headers."\r\n\r\n".$contents;
} else {
throw new RuntimeException("No connection exists");
}
}
/**
* Close the connection to the server
*
*/
public function close()
{
if (is_resource($this->stream)) {
ErrorHandler::start();
fclose($this->stream);
ErrorHandler::stop();
$this->stream = null;
}
}
/**
* Check if the socket has timed out - if so close connection and throw
* an exception
*
* #throws TimeoutException with READ_TIMEOUT code
*/
protected function _checkSocketReadTimeout()
{
if ($this->stream) {
$info = stream_get_meta_data($this->stream);
$timedout = $info['timed_out'];
if ($timedout) {
$this->close();
throw new TimeoutException(
"Read timed out after {$this->options['timeout']} seconds",
TimeoutException::READ_TIMEOUT
);
}
}
}
}
public function sendAndroidPushNotification($registration_ids, $message)
{
$registrationIds = array($registration_ids);
$msg = array(
'message' => $message,
'title' => 'notification center',
'vibrate' => 1,
'sound' => 1
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $msg
);
$fields = json_encode($fields);
$arrContextOptions=array(
"http" => array(
"method" => "POST",
"header" =>
"Authorization: key = <YOUR_APP_KEY>". "\r\n" .
"Content-Type: application/json". "\r\n",
"content" => $fields,
),
"ssl"=>array(
"allow_self_signed"=>true,
"verify_peer"=>false,
),
);
$arrContextOptions = stream_context_create($arrContextOptions);
$result = file_get_contents('https://android.googleapis.com/gcm/send', false, $arrContextOptions);
return $result;
}