Error using PHP in Azure Service Bus? - php

I am trying to connect to a service bus queue in Microsoft Azure using PHP, using the following code found on the Azure guide:
<?php
require_once 'vendor/autoload.php';
use MicrosoftAzure\Storage\Queue\QueueRestProxy;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Queue\Models\CreateMessageOptions;
$connectionString = "DefaultEndpointsProtocol=https;AccountName="name";AccountKey=key";
// Create queue REST proxy.
$queueClient = QueueRestProxy::createQueueService($connectionString);
try{
// Create message.
$builder = new ServicesBuilder();
$queueClient->createMessage("cmps297r1", "Hello World!");
}
catch(ServiceException $e){
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/library/azure/dd179446.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
?>
However, when I run it, I get this error:
Catchable fatal error: Argument 4 passed to MicrosoftAzure\Storage\Common\Internal\ServiceRestProxy::__construct() must be an instance of MicrosoftAzure\Storage\Common\Internal\Serialization\ISerializer, array given, called in /Applications/XAMPP/xamppfiles/htdocs/297R/vendor/microsoft/azure-storage/src/Queue/QueueRestProxy.php on line 110 and defined in /Applications/XAMPP/xamppfiles/htdocs/297R/vendor/microsoft/azure-storage/src/Common/Internal/ServiceRestProxy.php on line 77

The code you pasted looks fine to me (except that you didn't include use statement for ServicesBuilder class which would cause an "Class not found" error).
And as #Thuan Ng mentioned the code above belongs to Azure Storage Queue, not Service Bus Queue. You need to refer to this documentation How to use Service Bus queues with PHP if you are using Azure Service Bus.

I Know this is an old Post but you are trying to create a storage queue and not a service bus queue

Related

Connecting to ActiveCollab by SDK throws an error

I'm trying to connect to our cloud account, but I get the following error:
Fatal error: Uncaught Error: Class 'ActiveCollab\SDK\Authenticator\Cloud' not found in /www/htdocs/asdf/asdf.org/example-cloud.php:13 Stack trace: #0 {main} thrown in /www/htdocs/asf/asdf.org/example-cloud.php on line 13
require_once('https://app.activecollab.com/xxxxxx/autoload.php');
// Provide name of your company, name of the app that you are developing, your email address and password.
$authenticator = new \ActiveCollab\SDK\Authenticator\Cloud('xxx', 'My Awesome Application', 'vvv#ghf.de', '123qwe123*');
// Show all Active Collab 5 and up account that this user has access to.
print_r($authenticator->getAccounts());
// Show user details (first name, last name and avatar URL).
print_r($authenticator->getUser());
// Issue a token for account #123456789.
$token = $authenticator->issueToken('123456789');
// Did we get it?
if ($token instanceof \ActiveCollab\SDK\TokenInterface) {
print $token->getUrl() . "\n";
print $token->getToken() . "\n";
} else {
print "Invalid response\n";
die();
}
What did I wrong?
First line in your code example is not good. You should check the article on basic usage of Composer prior to continuing: https://getcomposer.org/doc/01-basic-usage.md
Once you have properly set up Composer in your development environment, required ActiveCollab SDK, and updated dependencies, your first line will most probably look like this:
require __DIR__ . '/vendor/autoload.php';
But you said:
Installation
If you choose to install this application with Composer instead of pulling down the git repository you will need to add a composer.json file to the location you would like to pull the repository down to featuring:
So I tried to pull down the repository s- or am I wrong ...
https://github.com/activecollab/activecollab-feather-sdk

App Engine - PHP env - 500 error while sending email

I am planning to send GCP cost to stakeholders. I am using app engine to host and send an email.
https://cloud.google.com/solutions/automated-email-cloud-billing-summary
I am using gcloudshell. I am getting below error.
Error: 0.0.0.1:52050 [500]: /?authuser=0&environment_name=default - Uncaught Error: Class 'google\appengine\api\mail\Message' not found in /home/project/billingemails/index.php:39
Stack trace:
#0 {main}
thrown in /home/project/billingemails/index.php on line 39'''
Code:
<?php
namespace Google\Cloud\Samples\Auth;
use Google\Auth\Credentials\GCECredentials;
use google\appengine\api\app_identity\AppIdentityService;
use Google\Cloud\BigQuery\BigQueryClient;
use google\appengine\api\mail\Message;
// Variables holding your values, replace before deploying
$datatable = "projectid.billing_data.gcp_billing_export_v1_000000_000000_000000";
$toemail = "gcpbilling#domain.com";
$fromemail = "your#domain.com";
$message = wordwrap($message, 70, "\r\n");
try {
$mailmsg = new Message();
$mailmsg->setSender($fromemail);
$mailmsg->addTo($toemail);
$mailmsg->setSubject('GCP Billing Summary');
$mailmsg->setTextBody($message);
$mailmsg->send();
printf('<br>---Mail sent successfully!---<br>');
}
catch (InvalidArgumentException $e) {
printf('<br>---Error occurred, could not send email---<br>');
}
?>
It seems by the stack trace - the below part of it - that the class Message is being missed.
Uncaught Error: Class 'google\appengine\api\mail\Message' not found in /home/project/billingemails/index.php:39
For this reason, please, be sure that you have this class well written and in the right folder, so your index.php file can call it correctly and send the email as intended. After this class is in the right place, you should be fine.

catching GuzzleHttp \ Exception \ ConnectException and showing a custom message

I couldn't get a work around to catch this error and showing a custom message to user,
I tried to use web3js with laravel
https://github.com/IlyasDeckers/web3php
and when I tried to use this
$eth = new \IlyasDeckers\Web3PHP\Ethereum(env('WEB3_URL'), env('WEB3_PORT'));
try {
$eth = $eth->eth_getBalance('0x8fbb99e9e73cd62bb3adea5365ff0f9d90c9e532', $block='latest', $decode_hex=false);
}
catch(ConnectException $e) {
echo 'Message: ' .$e->getMessage();
exit;
}
I am getting the error which cannot be caught, can anyone help on this?
GuzzleHttp \ Exception \ ConnectException cURL error 7: Failed to
connect to 127.0.0.1 port 8545: Connection refused (see
http://curl.haxx.se/libcurl/c/libcurl-errors.html)
It sounds very much to me like you aren't running an Ethereum node. It's trying to connect to localhost on port 8545.
If you are running a node, check the port!
If you aren't running a node, either set one up, or find one that will allow you to connect (far less likely tbh).
UPDATE
I notice you are catching a ConnectException, the fully qualified classname being GuzzleHttp\Exception\ConnectException.
If you haven't imported the class name using a use statement, then you need to change your code to this:
catch (\GuzzleHttp\Exception\ConnectException) {
(note the leading backslash). However, this isn't the recommended way. It's better to import all your classes at the top of your script:
use GuzzleHttp\Exception\ConnectException;
That way, you can instantly see all classes used in the script, and you can refer to the class as just ConnectException throughout your code.

Catching Exceptions from library using Laravel 5.2

Pretty new to laravel, so I'm not exactly sure how it handles errors and how best to catch them.
I'm using a 3rd party game server connection library that can query game servers in order to pull data such as players, current map etc..
This library is called Steam Condenser : https://github.com/koraktor/steam-condenser
I have imported this using composer in my project and all seems to be working fine, however I'm having trouble with catching exceptions that are thrown by the library.
One example is where the game server you are querying is offline.
Here is my code:
public function show($server_name)
{
try{
SteamSocket::setTimeout(3000);
$server = server::associatedServer($server_name);
$server_info = new SourceServer($server->server_ip);
$server_info->rconAuth($server->server_rcon);
$players = $server_info->getPlayers();
$total_players = count($players);
$more_info = $server_info->getServerInfo();
$maps = $server_info->rconExec('maps *');
preg_match_all("/(?<=fs\)).*?(?=\.bsp)/", $maps, $map_list);
}catch(SocketException $e){
dd("error");
}
return view('server', compact('server', 'server_info', 'total_players', 'players', 'more_info', 'map_list'));
}
If the server is offline, it will throw a SocketException, which I try to catch, however this never seems to happen. I then get the error page with the trace.
This causes a bit of a problem as I wish to simply tell the end user that the server is offline, however I cannot do this if I can't catch this error.
Is there something wrong with my try/catch? Does laravel handle catching errors in this way? Is this an issue with the 3rd party library?
A couple things:
Does the trace lead to the SocketException or to a different error? It's possible that a different error is being caught before the SocketException can be thrown.
Your catch statement is catching SocketException. Are you importing the full namespace at the top of your PHP file? use SteamCondenser\Exceptions\SocketException;
Also for debugging purposes, you could do an exception "catch all" and dump the type of exception:
try {
...
}catch(\Exception $e){
dd(get_class($e));
}
If you still get the stack trace after trying the above code, then an error is being thrown before the try/catch block starts.

PHP AMQP consumer: Server channel error: 404, message: NOT_FOUND

I'm using the amqp extension in pecl 1.0.3, compiled with 2.7.1 rabbitmq.
I'm trying to get a basic consumer/producer example working, but I keep getting errors. There's very little php documentation on this extension and a lot of it seemed to be outdated or wrong.
I used the code a user posted, but can't seem to get the consumer part working
Connection:
function amqp_connection() {
$amqpConnection = new AMQPConnection();
$amqpConnection->setLogin("guest");
$amqpConnection->setPassword("guest");
$amqpConnection->connect();
if(!$amqpConnection->isConnected()) {
die("Cannot connect to the broker, exiting !\n");
}
return $amqpConnection;
}
Sender:
function amqp_send($text, $routingKey, $exchangeName){
$amqpConnection = amqp_connection();
$channel = new AMQPChannel($amqpConnection);
$exchange = new AMQPExchange($channel);
$exchange->setName($exchangeName);
$exchange->setType("fanout");
if($message = $exchange->publish($text, $routingKey)){
echo "sent";
}
if (!$amqpConnection->disconnect()) {
throw new Exception("Could not disconnect !");
}
}
Receiver:
function amqp_receive($exchangeName, $routingKey, $queueName) {
$amqpConnection = amqp_connection();
$channel = new AMQPChannel($amqpConnection);
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$queue->bind($exchangeName, $routingKey);
//Grab the info
//...
}
Then sending it:
amqp_send("Abcdefg", "action", "amq.fanout");
And Receiving it:
amqp_receive("amq.fanout","action","action");
I keep getting a problem running the script and points to the amqp receive:
PHP Fatal error: Uncaught exception 'AMQPQueueException' with message 'Server channel error: 404, message: NOT_FOUND - no queue 'action' in vhost '/'' in /home/jamescowhen/test.php:21
Can anyone point me to the right direction? The whole sample is from a user note here:
http://www.php.net/manual/en/amqp.examples.php#109024
The exception seems to be caused by your queue not being declared (as the error message describes 404 - the queue 'action' was not found). The reason why the example works for the original poster is probably because he already has declared the queue earlier, without realizing that it's missing in his example.
You can declare the queue by calling ->declare() on the queue object. You'll also have to do this with the exchange object, unless you're certain that it already exists when you attempt to hook the queue onto it.

Categories