【问题标题】:Wordpress plugin WP_errorWordpress 插件 WP_error
【发布时间】:2015-09-06 04:21:16
【问题描述】:

我在 wordpress 中安装了 ISMS 插件,以便通过 Wordpress 试用 SMS 服务。但是,当我点击插件菜单上的 iSMS 设置时出现错误。

这是错误: ** 致命错误:无法在第 17 行的 C:\wamp\www\wordpress\wp-content\plugins\isms\isms-model.php 中将 WP_Error 类型的对象用作数组**

这是第 17 行的代码:

$result = $response[body];

这里是isms-model.php的完整代码

<?php

   class Mobiweb_ISMS_Model {
   // iSMS API
    protected static $api_balance = 'https://www.isms.com.my/isms_balance.php';
    protected static $api_send = 'https://www.isms.com.my/isms_send.php';

public static function get_balance() {
   $username = get_option('setting_username');
   $password = get_option('setting_password');

   $link = self::$api_balance.'?';
   $link .= "un=".urlencode($username);
   $link .= "&pwd=".urlencode($password);

   $response = wp_remote_get($link);
   $result = $response[body];
   $balance = (float)$result;

   if ($balance < 0) return substr($result, 8);
   else return $result;


   }

   public static function send_isms($destination, $message, $messageType, $senderID = '') {
   $username = get_option('setting_username');
   $password = get_option('setting_password');

   $link = self::$api_send.'?';
   $link .= "un=".urlencode($username);
   $link .= "&pwd=".urlencode($password);
   $link .= "&dstno=".urlencode($destination);
   $link .= "&msg=".urlencode($message);
   $link .= "&type=".urlencode($messageType);
   $link .= "&sendid=".urlencode($senderID);

   $response = wp_remote_get($link);
   try {
       $result = $response[body];

       $resultValue = (float)$result;
       if ($resultValue < 0) {
           return array(
               'code'=>$resultValue,
               'message'=>substr($result, 8)
           );
       } else {
           return array(
               'code'=>'2000',
               'message'=>$result
           );
       }
   } catch (Exception $e) {
       $message = $e->getMessage();
       return array(
           'code'=>'-9999',
           'message'=>$message
       );
   }
  }
 }

?>

我应该怎么做才能修复它?有什么建议吗?

【问题讨论】:

    标签: wordpress plugins


    【解决方案1】:

    这个插件写得不好。

    wp_remote_get() 在出现错误时返回 WP_Error 对象。因此,至少为了调试它并查看错误是什么,我建议您将其更改为:

    $response = wp_remote_get($link);
    $result = $response[body];
    

    $response = wp_remote_get($link);
    if (is_wp_error($response)) {
      die($response->get_error_message());
    }
    $result = $response['body'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 2017-02-06
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多