2016-10-01 15 views
0

Это мой код для пересылки по электронной почте:Outlook Mail Rest Api вперед Запрос по электронной почте возвращается статус 400

public static function sendForwardMessage($access_token, $user_email, $data, $message_id) { 
    $getMessagesUrl = self::$outlookApiUrl . "/Me/messages/" . $message_id . "/forward"; 

    return self::makeApiCall($access_token, $user_email, "POST", $getMessagesUrl, $data); 
} 

и это код CURL:

private static $outlookApiUrl = "https://outlook.office.com/api/v2.0"; 

public static function makeApiCall($access_token, $user_email, $method, $url, $payload = NULL) { 
    // Generate the list of headers to always send. 

    $headers = array(
     "User-Agent: php-tutorial/1.0", // Sending a User-Agent header is a best practice. 
     "Authorization: Bearer " . $access_token, // Always need our auth token! 
     "Accept: application/json", // Always accept JSON response. 
     "client-request-id: " . self::makeGuid(), // Stamp each new request with a new GUID. 
     "return-client-request-id: true", // Tell the server to include our request-id GUID in the response. 
     "X-AnchorMailbox: " . $user_email   // Provider user's email to optimize routing of API call 
    ); 

    $curl = curl_init($url); 

    switch (strtoupper($method)) { 
     case "GET": 
      // Nothing to do, GET is the default and needs no 
      // extra headers. 
      error_log("Doing GET"); 
      break; 
     case "POST": 
      error_log("Doing POST"); 
      // Add a Content-Type header (IMPORTANT!) 
      $headers[] = "Content-Type: application/json"; 
      curl_setopt($curl, CURLOPT_POST, true); 
      curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); 
      break; 
     case "PATCH": 
      error_log("Doing PATCH"); 
      // Add a Content-Type header (IMPORTANT!) 
      $headers[] = "Content-Type: application/json"; 
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); 
      curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); 
      break; 
     case "DELETE": 
      error_log("Doing DELETE"); 
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 
      break; 
     default: 
      error_log("INVALID METHOD: " . $method); 
      exit; 
    } 

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
    $response = curl_exec($curl); 
    error_log("curl_exec done."); 
    //echo "test";print_r($response); 
    $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    //echo "test".$httpCode; 
    error_log("Request returned status " . $httpCode); 

    if ($httpCode >= 400) { 
     return array('errorNumber' => $httpCode, 
      'error' => 'Request returned HTTP error ' . $httpCode); 
    } 

    $curl_errno = curl_errno($curl); 
    $curl_err = curl_error($curl); 

    if ($curl_errno) { 
     $msg = $curl_errno . ": " . $curl_err; 
     error_log("CURL returned an error: " . $msg); 
     curl_close($curl); 
     return array('errorNumber' => $curl_errno, 
      'error' => $msg); 
    } else { 
     error_log("Response: " . $response); 
     curl_close($curl); 
     return json_decode($response, true); 
    } 
} 

Это значение $ данных:

$arr= array(
    "Message" =>array(
    "Comment" => "Hi",  
    "ToRecipients"=>array(
    array(
     "EmailAddress"=>array(
      "Address"=>$forward_email, 
     ) 
    ) 
    ) 
)); 

$data=json_encode($arr, true); 

Тогда я вызываю функцию sendForwardMessage:

$message = OutlookService::sendForwardMessage($_SESSION['access_token'], $_SESSION['user_email'], $data, $message_id); 
print_r($message); 

Я проверил другое сообщение, связанное с этим. нажмите here Это не помогло.

Могу ли я узнать, что не так с моим кодом? и почему я получаю эту ошибку?

ответ

0

Скорее всего это бит Message на вашем массиве. Если я не ошибаюсь, это будет сериализовать к чему-то вроде:

{ 
    "Message": { 
    "Comment": "Hi", 
    "ToRecipients": [ 
     { 
     "EmailAddress": { 
      "Address": "[email protected]" 
     } 
     } 
    ] 
    } 
} 

Согласно API reference for Forward, она должна выглядеть как:

{ 
    "Comment": "Hi", 
    "ToRecipients": [ 
    { 
     "EmailAddress": { 
     "Address": "[email protected]" 
     } 
    } 
    ] 
} 

Попробуйте этот код вместо:

$arr= array(
    "Comment" => "Hi",  
    "ToRecipients"=>array(
    array(
     "EmailAddress"=>array(
     "Address"=>$forward_email, 
    ) 
    ) 
) 
);