2014-03-31 155 views
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實際上得到執行?就在函數被調用的時候? 非常感謝提前幫助;)

+0

超級有用-.- @Hobo –

+0

你有任何的錯誤代碼?錯誤日誌中是否顯示任何內容?我不知道你在做什麼錯了。你的'$ query'變量中有什麼?還im我不太確定你爲什麼在錯誤區域運行curl_close()。 – Stewartside

回答

2

嘗試了這一點,我看看你得到的是什麼

$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"); 

    throw new FBAOutboundServiceMWS_Exception(
     array(
      'Message' => $errorResponse, 
      'ErrorType' => 'HTTP' 
     ) 
    ); 
} else { 
    echo 'Response:<pre>'; 
    print_r($response); 
    echo '</pre>'; 
} 

掏出你不需要的代碼,還增加了在響應印刷這你以前沒有做過。

+0

這使我回到了我的答案,'wp_remote_post'在一個數組中返回了我的數據,所以我必須改變返回答案的使用方式。感謝您的協助。 –