How to retrieve error messages from Sabre API - sabre

I am using the getReservationRQ API (I am able to retrieve other info from the API) but I am not able to echo out the error messages. Does anyone have any ideas? Please see my code below.
private function handle_api_error($xml) {
//echo "<textarea>".$xml->asXML()."</textarea>";
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap-env'])->Body;
//$this->echo_xml($soap);
foreach($soap->children($ns['stl17'])->GetReservationRS->Errors as $error) {
$error_msg .= $error->Message.'<br />';
}
$this->error_msg="Sabre SOAP API error: ".$error_msg;
return false;
}

I recommend you to use some tool like Postman or SoapUI to see the actual error you are getting.
My guess is that it might be a different namespace: $ns['???']

Related

Symfony: Webhook End Point Problems

I am having trouble with Webhook Endpoints.
#[Route("/web_hook", name: 'web_hook')]
public function webhook(Request $request)
{
$data = json_decode($request->getContent(), true);
if ($data === null){
throw new \Exception('bad bad');
}
$txt = 'samle of some text text text';
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fwrite($myfile, $data->id);
fclose($myfile);
$response = new Response();
$response->setStatusCode(200);
return $response;
}
I am having 0 luck getting it to work, I am sending a POST request to it VIA Postman and it keeps coming back with error 500.
When I visit then URL it gives me this error
"Cannot autowire argument $request of "App\Controller\WebHookController::webhook()": it references class "Symfony\Component\HTTPFoundation\Request" but no such service exists.
"
I based this method off of
https://symfonycasts.com/screencast/stripe-level2/webhook-endpoint-setup.
any ideas on what im doing wrong would be greatly appreciated.
if you get error message like this
Cannot autowire argument $request of
"App\Controller\WebHookController::webhook()": it references class
"Symfony\Component\HTTPFoundation\Request" but no such service exists.
mean this service is not available. please try install http-foundation component first.
composer require symfony/http-foundation
and try to change use Symfony\Component\HTTPFoundation\Request to use Symfony\Component\HttpFoundation\Request

Symfony 2.8 : Get message command using Process in controller

I have a file command handling data add database . I use Document Symfony 2.8 Process for the handling in the controller.
Everything was running fine. But I got an error . In the file command I use if-else to check data. When processed and running $process->run() , I cannot get the error in else (in else. I use echo to show the error).
if (!empty($id)) {
// handling code
} else {
echo "not found Id"
}
Do you have any ideas? help me please !
Document i use : https://symfony.com/doc/current/components/process.html
Normaly you can get the output of the process by using : echo $process->getOutput(); or in your case you can throw exception like
if (!empty($id)) {
// handling code
} else {
throw new \Exception('Id not found');
}
and put your process within try catch block like that
try {
$process->mustRun();
echo $process->getOutput();
} catch (\Exception $exception) {
echo $exception->getMessage();
}

Set telegram webhook in custom module on drupal 8

I created a module and implemented my Telegram bot code in /src/Controller/BotController.php. Now I want to set a webhook and use https://api.telegram.org/bot<token>/setwebhook?url=https://<my-site>/<my-module-path-in-"name.routing.yml">.
The webhook is set, but my code doesn't work and gives me a 500 error.
How can I fix this?
EDIT:
My code:
<?php
namespace Drupal\telegram\Controller;
use Drupal\Core\Controller\ControllerBase;
class TelegramController extends ControllerBase {
public function telegram() {
$update = file_get_contents("php://input");
$update = json_decode($update, TRUE);
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
if (isset($chatId) and isset($message)) {
sendMessage($chatId, $message);
$url = "https://api.telegram.org/bot504387877:AAFHnQe-AdscpSZN42yY-JYem5jwdJc131Q/sendMessage?chat_id=" . $chatId . "&text=" . $message;
}
file_get_contents($url);
$build['#theme'] = 'new';
return $build;
}
}
I think it may cause by unescaped URL, but you need to provide error message to determine.
If you just want to make it done, you can use this Android application. For instance, use getWebhookInfo to obtain error reason.

Receive Bearer Token from API with R

I'm searching for a solution to receive a Bearer token from an API using username and password.
Right now I'm reading the token through Chrome and extract my data, which is less then ideal of course.
I tried with httr and curl to optain through R and receive the Bearer token, but i think i am quite lost.
I think it should be quite simple, from the login information i gathered the mask from the login as
{"username":"name","password":"pw"}, shouldn't this just work with the POST command and the right headers?
POST(url="api_login",config=add_headers(c("username: name"
,"password: pw")))
Doesn't work at all. I can provide the example for php which looks like this:
<?php
// Include Request and Response classes
$url = 'url';
$params = array(
'username' => 'sample_username',
'password' => 'sample_password'
);
// Create a new Request object
$request = new Request($url, 'POST', $params);
// Send the request
$request->send();
// Get the Response object
$response = $request->getResponse();
if($response->getStatusCode() == 200) {
print_r($response->getBodyDecoded());
}
else {
echo $response->getStatusCode() . PHP_EOL;
echo $response->getReasonPhrase() . PHP_EOL;
echo $response->getBody() . PHP_EOL;
}
?>
As I'm not very familiar with php i would be very pleased for any help or a guide into the right direction. I searched hours
for API access through R but everything looks very specific to a special login.
I figured out this API uses a deprecated version of Swagger, if this is any useful information.
Thats what I'm doing atm, login with the website and read the token out of my browser. I want to login from inside R, sorry if I wasn't clear.
I updated my code now to:
opts=curlOptions(verbose=TRUE,
ssl.verifypeer = T)
postForm(url,
"username:" = uname, "password:"=pswd,
httpheader = c('Content-Type' = 'application/json', Accept = 'application/json'),
.opts=opts,
style='POST'
)
Which results in an error: SSL certificate problem: self signed certificate in certificate chain.
I tried a lot of different certificates with 'cainfo' inside the argument but can't make it work.

Supress Error messages in Symfony/Twig?

How can I suppress error messages in a symfony controller / swift message, e.g. if the email of the user doesn't exist, something is wrong with the smpt server or the email address simply doesn't comply with RFC 2822.
For example, we got following CRITICAL error...
request.CRITICAL: Swift_RfcComplianceException: Address in mailbox given [ xxx#xxx.com ] does not comply with RFC 2822, 3.6.2. (uncaught exception) at $
... the user then get's a symfony error page "An Error occured" which I need to suppress in any case.
A simple #$this->get('mailer')->send($message); doesn't work here unfortunately ...
protected function generateEmail($name)
{
$user = $this->getDoctrine()
->getRepository('XXX')
->findOneBy(array('name' => $name));
if (!$user) {
exit();
}
else {
$message = \Swift_Message::newInstance()
->setSubject('xxx')
->setFrom(array('xxx#xxx.com' => 'xxx'))
->setTo($user->getEmail())
->setContentType('text/html')
->setBody(
$this->renderView(
'AcmeBundle:Template:mail/confirmed.html.twig'
)
)
;
$this->get('mailer')->send($message);
// a simple #$this->get('mailer')->send($message); doesn't work here
}
return true;
}
To simply suppress the error, wrap the send method in a try-catch block. Choose the exception type wisely. The following example just catches Swift_RfcComplicanceExceptions.
try {
$this->get('mailer')->send($message);
} catch (\Swift_RfcComplianceException $exception) {
// do something like proper error handling or log this error somewhere
}
I'd consider it advisably to apply some validation beforehand, so you can display a nice error to your user.
I'm not sure that a try catch block will work, because mails may be sent later in the request process : http://symfony.com/fr/doc/current/components/http_kernel/introduction.html#l-evenement-kernel-terminate
If you use the framework Full Stack edition, this is the default behavior.
I've also found some refs here : Unable to send e-mail from within custom Symfony2 command but can from elsewhere in app
You can change the Swiftmailer spool strategy in your config to send direct mails...

Resources