0
我需要將PHP庫從CURL轉換爲wp_remote_post以用於Wordpress插件。將CURL請求轉換爲Wordpress wp_remote_post
我參考了Wordpress wp_remote_post page here。
這是我嘗試轉換的代碼..
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $scheme . $url['host'] . $url['path']);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, $this->_config['UserAgent']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = "";
$response = curl_exec($ch);
if ($response === false) {
$errorResponse = curl_error($ch);
require_once("FBAOutboundServiceMWS/Exception.php");
curl_close($ch);
throw new FBAOutboundServiceMWS_Exception(array(
'Message' => $errorResponse,
'ErrorType' => 'HTTP'
));
}
curl_close($ch);
這是我「想」是正確的,但沒有工作...
$THEurl = $scheme . $url['host'] . $url['path'];
$response = wp_remote_post($THEurl, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => $query,
'cookies' => array()
)
);
if (is_wp_error($response)) {
$errorResponse = $response->get_error_message();
require_once("FBAOutboundServiceMWS/Exception.php");
curl_close($ch);
throw new FBAOutboundServiceMWS_Exception(array(
'Message' => $errorResponse,
'ErrorType' => 'HTTP'
));
}
在什麼時候wp_remote_post
實際上得到執行?就在函數被調用的時候? 非常感謝提前幫助;)
超級有用-.- @Hobo –
你有任何的錯誤代碼?錯誤日誌中是否顯示任何內容?我不知道你在做什麼錯了。你的'$ query'變量中有什麼?還im我不太確定你爲什麼在錯誤區域運行curl_close()。 – Stewartside