我正在通过Symfony发送短信,我通过ovh API,创建了一个表单来键入要发送的消息,并且可以显示它,但是当我单击send时,出现此错误: 不能将Symfony \ Bridge \ Monolog \ Logger类型的对象用作数组
问题是当我尝试连接到api ovh时 感谢您的帮助
我的服务:
class SmsProvider
{
/** @var array */
private $config;
/** @var LoggerInterface */
private $logger;
public function __construct($config, LoggerInterface $logger)
{
$this->config = $config;
$this->logger = $logger;
}
public function sendMessage($phoneNumbers, $message)
{
$conn = $this->connectToApi();
$phoneNumbers = (array)$phoneNumbers;
$content = [
'charset' => 'UTF-8',
'class' => 'phoneDisplay',
'coding' => '7bit',
'message' => $message,
'noStopClause' => true,
'priority' => 'high',
'receivers' => $phoneNumbers,
'senderForResponse' => true,
];
try {
$smsServices = $conn->get('/sms/');
$result = $conn->post(sprintf('/sms/%s/jobs/',
$smsServices[0]), $content);
} catch (\Exception $e) {
if (null !== $this->logger) {
$this->logger->critical(
sprintf("Erreur lors de l'envoi SMS : %s . Trace :
%s", $e->getMessage(), $e->getTraceAsString()), [
'paramsOvh' => $content
]
);
}
$result = false;
}
return $result;
}
private function connectToApi()
{
if (!isset($this->config['application_key'],
$this->config['application_secret'],
$this->config['consumer_key'],
$this->config['end_point'])
) {
$this->logger->error('OVH config parameters are missing');
throw new \Exception('OVH config parameters are missing');
}
$applicationKey = $this->config['application_key'];
$applicationSecret = $this->config['application_secret'];
$consumerKey = $this->config['consumer_key'];
$endPoint = $this->config['end_point'];
try {
$conn = new Api(
$applicationKey,
$applicationSecret,
$endPoint,
$consumerKey
);
} catch (InvalidParameterException $e) {
$this->logger->critical(
sprintf("Erreur lors de la connexion à l'API OVH : %s .
Trace : %s", $e->getMessage(), $e->getTraceAsString())
);
throw $e;
}
return $conn;
}
}
控制器:
public function sendSmsAction(Request $request)
{
$telephones = $request->get('telephone');
$form = $this->createForm(smsFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message = $form->get('message')->getData();
$smsProvider = $this->get('app.sms.provider');
$smsProvider->sendMessage($message, $telephones);
}
return $this->render('
CeUtilisateurBundle:Utilisateur:sms.html.twig', array(
'form' => $form->createView()));
}
service.yml
app.sms.provider:
class: Ce\UtilisateurBundle\Services\SmsProvider
arguments: ['@logger']
public: true
答案 0 :(得分:0)
IMO,您应该创建另一个类,该类必须返回一个正确配置为进行通信的Api
对象。
因此,此服务需要3个标量参数(或您的配置数组)
您应该以这种方式定义
app.ovh_connection:
class: Ce\UtilisateurBundle\Services\OvhConnection
arguments:
$applicationKey: '%application_key%'
$applicationSecret: '%application_secret%'
$consumerKey: '%consumer_key%'
处理连接的类如下
class OvhConnection {
private $applicationKey;
private $applicationSecret;
private $consumerKey;
constructor($applicationKey, $applicationSecret, $consumerKey) {
$this->applicationKey = $applicationKey;
$this->applicationSecret = $applicationSecret;
$this->consumerKey = $consumerKey;
}
public function getConnection($endpoint) {
return new Api(
$this->applicationKey,
$this->applicationSecret,
$endPoint,
$this->consumerKey
);
}
}
非常简单,因此仅在此处处理连接部分。如果您想使用该API的其他服务(例如电子邮件发送),则只需使用此类。
然后,您必须调整SmsProvider类以从新类中检索连接
class SmsProvider
{
/** @var OvhConnection */
private $connection;
/** @var LoggerInterface */
private $logger;
public function __construct(OvhConnection $connection, LoggerInterface $logger)
{
$this->connection = $connection;
$this->logger = $logger;
}
// Call $this->connection->getConnection('your_endpoint'); to retrieve an OvhConnection configured
}
最后,您必须以这种方式更新SmsProvider
的定义
app.sms.provider:
class: Ce\UtilisateurBundle\Services\SmsProvider
arguments: ['@app.ovh_connection', '@logger']
public: true