2013-06-18 129 views
2

我正在嘗試將curl命令轉換爲wp_remote_request命令。
這裏是curl命令:curl命令wp_remote_request命令

curl -v 
-H "Accept:application/json" -H "Content-type:application/json" 
-X POST -d '{"user":{"password":"***","email":"***"}}' 
http://***/users/sign_in.json 

這裏是我的PHP

$t = array(
    "user" => array(
       "password" => "***", 
       "email" => "***")); 

$args = array (  
    'headers' => 
    array (
     'Accept'  => 'application/json', 
     'Content-Type' => 'application/json', 
    ), 
    'method' => 'POST', 
    'body'  => json_encode($t) 
); 

$response = wp_remote_request( 
      'http://***/users/sign_in.json' , $args); 

的問題是,它只是將無法正常工作。根據我在「身體」中放置的內容,我得到了不同的錯誤,但通常只是'404'。我唯一能想到的是curl -d以某種方式編碼請求,但我無法弄清楚如何。有什麼想法嗎?謝謝。

順便說一句,下面的工作正常,但我再次想用wp_remote_request

$t = array('user' => array('password' => '***', 
          'email' => '***')); 
$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => 'http://***/users/sign_in.json', 
    CURLOPT_POST => 1, 
    CURLOPT_POSTFIELDS => json_encode($t), 
    CURLOPT_HTTPHEADER => array('Content-Type: application/json') 
)); 

$resp = curl_exec($curl); 
curl_close($curl); 

回答

0

我昨晚戰鬥wp_remote_request。我的認證是在標題,如果你說的curl_setopt_array版本的工作,比我們的問題是不同的,但儘量包含內容長度

$headers = array(
     'Authorization' => 'Basic ' . base64_encode($this->key.':'.$this->password), 
     'Accept'  => 'application/json', 
     'Content-Type' => 'application/json', 
     'Content-Length' => strlen(json_encode($body)) 
    ); 

    // Setup variable for wp_remote_post 
    $post = array(
     'method' => 'POST', 
     'headers' => $headers, 
     'body'  => json_encode($body) 
    ); 
0

你不想要json_encode身體。不知道該方法是否會自動執行該操作或該操作(看起來很奇怪)。從body變量中刪除你的json_encode,它應該可以工作。