无法通过magento 2.0中的rest api创建发票

时间:2016-05-10 16:32:15

标签: php rest curl magento-2.0

我有以下代码为magento 2.0中的订单创建发票

<?php
$adminUrl='http://xxxxx/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "xxxxx", "password" => "xxxxx");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
     'Content-Type: application/json',                                                                                
     'Content-Length: ' . strlen($data_string))                                                                       
      );       
$token = curl_exec($ch);
$token=  json_decode($token); 
$headers = array("Authorization: Bearer $token"); 
$requestUrl1='http://xxxxx/index.php/rest/V1/invoices';
$ch1 = curl_init();
$ch1 = curl_init($requestUrl1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
$result1 = curl_exec($ch1);
$result1=  json_decode($result1);
print_r($result1);
?>

它给了我

{"message":"%fieldName is a required field.","parameters":{"fieldName":"entity"}} 

1 个答案:

答案 0 :(得分:0)

尝试传递像这样的发票数据

    $setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);

    $productData='{"entity":{"orderId":6,"items":[{"orderItemId":9,"qty":1},{"orderItemId":10,"qty":1}],"comments":[],"extensionAttributes":{}}}';

    $requestURL= "{magento-url}/rest//V1/invoices";

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $requestURL);

    curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);