2017-11-10 2 views
0

WordPressのコア機能を使用しようとしていますwp_remote_postサーバーにデータを投稿し、そこから返信します。私はPHPとWordPressにはかなり新しいです。私が次のコードを実行しているときは、WordPress:キャッチ可能な致命的なエラー:クラスstdClassのオブジェクトを文字列に変換できませんでした

Catchable fatal error: Object of class stdClass could not be converted to string in /var/www/wp-projects/csrvtool.com/wp-content/themes/yrc_csrvtool/ajax.php on line 146

です。このライン: tt_platform_api_wordpress_request($url_send, $arr, 'helper/ping', 120);

/** 
* Wordpress request handler 
* @param string $url 
* @param array $params 
* @param string $endpoint 
* @param int $timeout in seconds 
* @return object|string 
*/ 

$url_send = "https://api.emailer.traveltime.com/v1/"; 

    $arr = array(
     'accessToken' => 'xxxx-xxxx-xxxx-xxxx', 
     'username' => 'bob', 
     'location' => array(
      'city' => 'Kansas City', 
      'state' => 'Missouri' 
     ) 
    ); 

    tt_platform_api_wordpress_request($url_send, $arr, 'helper/ping', 120); 


    function tt_platform_api_wordpress_request($url, $params, $endpoint, $timeout) { 
     if(!function_exists('wp_remote_post')) return FALSE; 

     $response = wp_remote_post(
      $url, 
      array(
       'timeout' => $timeout, 
       'headers' => array('Content-Type' => 'application/json'), 
       'body' => json_encode((object) $params) 
      ) 
     ); 
     if(!is_wp_error($response)) { 
      return (object) array(
       'data' => $response['body'], 
       'code' => $response['response']['code'] 
      ); 
     } else { 
      return 'wp_remote_post returned an error for ' . $endpoint . ': ' . var_export($response, TRUE); 
     } 
    } 

EDIT:

object(stdClass)#546 (3) { ["accessToken"]=> string(36) "76A4412A-4737-4DB7-9098-4D4698FE895C" ["username"]=> string(3) "bob" ["location"]=> array(2) { ["city"]=> string(11) "Kansas City" ["state"]=> string(8) "Missouri" } } 

私が間違っているのかを理解するために私を助け、右は何かしてください:以下の出力私はvar_dump ((object) $arr);を試してみました

それを行う方法。

+0

あなたの質問を編集して、完全なエラーメッセージに行番号を追加してください。 – mmm

+0

@mmm行番号の完全なエラーメッセージを追加しました。 –

+0

@JordiNebotあなたの提案を無駄なく試してみました! :(まだ同じエラーが発生しています –

答えて

-1

あなたはオブジェクトへの応答をタイプし、それを配列としてアクセスしようとしているようです。 (オブジェクト)を削除し、レスポンスを通常の配列として使用します。希望が役立ちます。

+0

ありがとう!(オブジェクト)レスポンスから型キャストを削除した後、私は 'Notice:Array to string conversion to /var/www/wp-projects/csrvtool.com/wp-content/themes/yrc_csrvtool /ajax.php on line 146 Array' –

+0

$ responseをvar_dumpできますか? – Thomas

0

あなたの投稿は、実際にオブジェクトを文字列にキャストした部分を表示していないようです。

おそらく、あなたのコードに従ってstdClass(成功した場合)の結果をtt_platform_api_wordpress_requestとしています。おそらく、それをエコーし​​たり、それを連結したり、それを文字列として扱う他の関数に渡したりしているかもしれません。あなたのコードはこれを表示しません。

stdClass(またはそれに関する配列)を文字列として使用しないでください。それをそのまま扱う。

関連する問題