Wyy
Wyy

Reputation: 11

Wordpress plugin WP_error

I installed ISMS Plugin into my wordpress in order to try out the SMS service through Wordpress. However an error appeared when I click on the iSMS setting on the plugin menu.

Here is the error: ** Fatal error: Cannot use object of type WP_Error as array in C:\wamp\www\wordpress\wp-content\plugins\isms\isms-model.php on line 17**

and here's the code for line 17:

$result = $response[body];

here is the full code for 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
       );
   }
  }
 }

?>

What should I do to fix it? Any advise?

Upvotes: 0

Views: 408

Answers (1)

uri2x
uri2x

Reputation: 3202

This plugin is badly written.

wp_remote_get() returns a WP_Error object when there's an error. Therefore, at least for debugging it and seeing what the error is, I would suggest you change it from:

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

to

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

Upvotes: 2

Related Questions