Use twitchAPI with cake - cakephp

since a few hours i'm trying to implement the twitchAPI in my cake projet. a long time ago i made this little script in basic php.
$channelName = "gamespot";
$json_file = #file_get_contents("http://api.justin.tv/api/stream/list.json?channel={$channelName}", 0, null, null);
$json_array = json_decode($json_file, true);
#$json_array[0] && $json_array[0]['name'] == "live_user_{$channelName}";
#$title = $json_array[0]['channel']['status'];
#$game = $json_array[0]['meta_game'];
#$chanel_view = $json_array[0]['channel_count'];
#$totalchanelview = $json_array[0]['channel_view_count'];
but i don't know how to add this lines on my controller
For know i've just find this
public function twitch() {
$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=manvsgame');
$twitch = json_decode($json);
$totalchanelview = $twitch[0]['channel_view_count'];
$this->set('twitch', 'totalchanelview');
}
but of course i've this error
Fatal error: Cannot use object of type stdClass as array in /Users/*/Desktop/Websites/**/app/Controller/UsersController.php on line 29
anyone can explain to me how i can use this API?
thanks in advance and have a nice day/night :)
okey first thanks to help me. i still have a little "logic problem"
my function is something like that:
public function twitch() {
$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=gamespot');
$twitch = json_decode($json, true);
$this->set('json', $twitch);
}
but know, what can I write to my view to see my informations (like the title of my stream for exemple.
I test with
echo $twitch[0]['title']; (it's my line 1)
bit i've this error
Notice (8): Undefined variable: twitch [APP/View/Users/admin_dashboard.ctp, line 1]

$twitch = json_decode($json, true); // add true param
$twitch[0]['channel_view_count'];
adding true returns the data as an associated array instead

Related

Laravel 7: Showing error while passing multiple variable in str_replace

I'm facing error while passing multiple variable in str_replace function.
Error: Argument 1 passed to Xenon\LaravelBDSms\SMS::shoot() must be of the type string, null given, called in
Message Body:
Hello #name#,
Total Amount Purchased : #total#
Previous Due: #previous_due#
Deposit: #deposit#
Total Due: #total_due#
Controller:
$id = 1;
$sms_settings = SmsSetting::findOrFail($id);
if($sms_settings->order_create == 1){
$name = $request->name;
$previous_due = $customer->due;
$deposit = $request->deposit;
$total = $request->total;
$total_due = $request->total_due;
$msgs = $sms_settings->order_create_sms;
$msg = str_replace(array('#name#', '#total#','#previous_due#','#deposit#','#total_due#'), array($name,$previous_due, $deposit, $total, $total_due), $msgs);
$send= SMS::shoot($request->mobile, $msg);
}
Shoot Function:
public function shoot(string $number, string $text)
{
$this->sender->setMobile($number);
$this->sender->setMessage($text);
return $this->sender->send();
}
Here I'm using a Laravel Package for sending SMS to mobile number. How can I pass multiple variable in str_replace?
$request->mobile is null, confirm if you are passing the same in the request. Thats why the error.
Also use $request->validated('mobile'), that is safer.
str_replace seems to be fine. Take a look at Example, but Look at examples again, it might break if characters are overlapping with other arguments
I think the variable $msgs = $sms_settings->order_create_sms; contain empty that's why str_replace couldn't replace the data that you given so
$msg = str_replace(array('#name#', '#total#','#previous_due#','#deposit#','#total_due#'), array($name,$previous_due, $deposit, $total, $total_due), $msgs); , will return null.
I recommend checking $msgs again.
$msgs = $sms_settings->order_create_sms;
Make sure $msgs is not null place is_null($msgs) condition before feeding to str_replace
check more about str_replace: https://www.php.net/manual/en/function.str-replace.php

PHP mailer and FPDF attachment with loop - script stops after first run

I'm running a following script to generate and send email. The email body is generated in a while loop (content differs) - it works fine. But now I have tried to include a script to generate PDF attachment (via FPDF library), in each iteration the attachment is different.
Problem is: the loop runs just once, for the first case and after it stops. Thank you for your commnents in advance.
My code:
<?
$mail = new PHPMailer();
$mail->SMTPDebug = 1;
$mail->isSMTP();
$mail->addReplyTo('');
$mail->isHTML(true);
$mail->Subject = "";
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->CharSet = 'utf-8';
$mail->setFrom('');
while(($data=MySQL_Fetch_Array($vysl))!=NULL) {
require_once('invoicetopdf.php');
$message="";
$mail->AddStringAttachment($invoice, 'Invoice.pdf', 'base64', 'application/pdf');
$mail->Username = "";
$mail->Password = "";
$mail->addAddress($to);
$mail->Body = $message;
if (!$mail->send()) {echo "Mailer Error: " . $mail->ErrorInfo;}
else {
$mail->clearAddresses();
$mail->ClearAllRecipients();
$mail->clearAttachments();
echo "Ok";
}
} //while
//invoicetopdf.php:
$data = MySQL_Fetch_Array($vysl);
require_once('../knihovny/pdf/fpdf.php');
$pdf = new PDF();
$pdf->.....;
$invoice=$pdf->Output('S');
?>
That's a bit of an odd way to run that code repeatedly. I would define a function in your invoicetopdf.php file, load it at the top of your script, and then call the function inside the loop to get the PDF data. You're also calling mysql_fetch_array twice - once in the while loop, once in the function, meaning half your data will be going astray.
require_once('invoicetopdf.php');
while(($data=MySQL_Fetch_Array($vysl))!=NULL) {
$message="";
$mail->AddStringAttachment(generatePDF($data), 'Invoice.pdf', 'base64', 'application/pdf');
...
//invoicetopdf.php:
require_once('../knihovny/pdf/fpdf.php');
function generatePDF($data) {
$pdf = new PDF();
$pdf->.....;
return $invoice=$pdf->Output('S');
}
I also recommend moving the Username and Password out of the loop, and you probably don't need to call clearAllRecipients(); clearAddresses() is enough.
Setting SMTPDebug = 2 will let you see more of what's happening in SMTP land.
Now it works: the main problem was a mixing a class and functions together. See:Multiple PDFs in Loop with FPDF
Thank you guys!

how to set limit start in joomla from custom library

I have custom library and need to set limit start for list view records from this library.
Code is as follows:
$limitStart = $input->json->get('limit_start');
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$model->setState("list.limit", $limitStart);
I did this but it's set default value to zero.
Can we override the limit start in joomla.
Thanks in advance.
I think you need to use following method to set the limitstart
$limitStart = 5;
$app = JFactory::getApplication();
$app->setUserState($this->context . '.limitstart', $limitStart);
Yes Can override the limitstart for your own library joomla framework
please flow this way
Open =>joomlaFile/configuration.php/for this code line no "7" default set 20
public $list_limit = '20'
Can you change to your Own PageLimit
public $list_limit = '5'
//simply read this
$config = JFactory::getConfig();
$limitStart = $config->get('list_limit');
you need system plugin to do this. see example for native tags component (the condition is needed to avoid rune code on wrong place )
public function onAfterRoute()
{
$app = JFactory::getApplication();
if ($app->input->getRaw('option') == 'com_tags' && $app->input->getRaw('view') == 'tag') {
$app->set('list_limit', 12);
}
}

How to Refresh Auth data after update

i m working classifieds controller
this is code
public function create_add(){
if(!empty($this->data))
{
if($this->Classified->save($this->data)){
$this->User->save(array('id'=>$this->Auth->User('id'),'pending_attempts'=>$this->Auth->User('pending_attempts')-1));
$data = $this->User->read();
$this->Auth->login($data);
}
}
}
i want to updating the 'pending_attempts' column in user table , for example:
pending_attempts = 10;
Every time run this code, pending_attempts will be 10 - 1, and always resulting in 9.
After login only time code work and update the database , if i again run that code create_add() it always show pending_attempts =9
Problem is that how to refresh Auth data ?
You are no updating the 'pending_attempts' , for example:
pending_attempts = 10;
Every time you run this code, pending_attempts will be 10 - 1, and always resulting in 9.
'pending_attempts'=>$this->Auth->User('pending_attempts')-1)
Looking at you code, i think you are trying to update user Auth session, but i think it's no working:
$this->Auth->login($data);
take a look here
I tried many of the tricks to refresh in my appplication i have done in last year.But nothing helped me.So why i can give you an alternative.Fetch data from table at first with user_id which is in session.And then use it as data for operation.....Happy coding bro...
Your method should looks like:
public function create_add()
{
if(!empty($this->data))
{
if($this->Classified->save($this->data))
{
$user_data = $this->User->find(null, $this->Auth->User('id'));
$user_data['User']['pending_attempts'] -= 1;
$this->User->save($user_data);
$this->Auth->login($user_data);
}
}
}
I checked it and it is working. Kindly ask if it is not working for you.
I got the solution of my question , that code working fine for me
public function create_add()
{
if(!empty($this->data))
{
if($this->Classified->save($this->data))
{
$user_data = $this->User->find('first',array('fields' => array('id','pending_attempts'),'conditions'=>array('User.id'=>$this->Auth->User('id'))));
$this->data['User']['id'] = $this->Auth->User('id');
$this->data['User']['pending_attempts']= $user_data['User']['pending_attempts']-1;
$this->User->save($this->data);
}
}
}

Prestashop 1.4.3 I need to get a database value and use it in MailAlerts module

I have asked this question on the Prestashop forum, but haven't had a reply as of yet.
I need to be able to add a sagepay code to the new order email used in mailalert module.
What I have is;
// Filling-in vars for email
$template = 'new_order';
$subject = $this->l('New order');
$spvtxc = Db::getInstance()->ExecuteS("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '$order->id_cart'");
...
$templateVars = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
...
'{sagepay_no}' => $spvtxc,
...
);
Every time i test a transaction the $spvtxc returns 'ARRAY'.
I have tried;
$spvtxc = '5';
As Expected this returns 5 as the sagepay number, so I'm confident the variables are being called and added to the email.
And I tried;
$spvtxc = Db::getInstance()->ExecuteS("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '2'");
So this should set $spvtxc to the value that is definatly there (i manually added it in the database), but this still returned 'ARRAY'.
If anyone can point out what I have missed, it is greatly appriceated.
As I was only needing to return a single value, I should have used getValue function instead of ExecuteS.
$spvtxc = Db::getInstance()->getValue("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '$order->id_cart'");
This returned the value.

Resources