【问题标题】:I can not connect to the Ovh API to send sms我无法连接到 Ovh API 发送短信
【发布时间】:2019-04-09 14:19:47
【问题描述】:

我正在Symfony上发送短信,我通过ovh API,我创建了一个表单来输入要发送的消息,我可以显示它但是当我点击发送时,我有这个错误: 不能使用 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()));
      }

服务.yml

  app.sms.provider:
             class: Ce\UtilisateurBundle\Services\SmsProvider
             arguments: ['@logger']
             public: true

【问题讨论】:

  • 你确定你放弃了所有的代码吗?你有线路和文件错误吗?您对记录器的使用似乎没问题
  • 感谢您的回答,我知道我必须在 service.yml 中传递两个参数,因为我在 smsProvider 服务的构造函数中传递了两个参数,但我不知道该怎么做

标签: api symfony service ovh


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多