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);
を試してみました
それを行う方法。
あなたの質問を編集して、完全なエラーメッセージに行番号を追加してください。 – mmm
@mmm行番号の完全なエラーメッセージを追加しました。 –
@JordiNebotあなたの提案を無駄なく試してみました! :(まだ同じエラーが発生しています –