2014-04-08 67 views
0

我正在SmallInvoice api上工作。这里是链接小发票API卷曲请求

smallinvoice.ch/api/general/overview

我试图在小发票帐户添加客户端。这里是代码

$ url ='https://api.smallinvoice.com/client/add/token/MyTokenIsHere';

  $address[] = array(
      "street" => "Address", 
         "streetno" => "", 
       "code" => 60000, 
         "city" =>"Multan", 
         "country" => "PK" 
      ); 
     $this->data['add_client'] = array(
      "type" => 2, 
      "gender" => 1, 
      "name" => "Adnan Ahmad", 
      "language" => "en", 
      "number" => 5, 
      "addresses" => $address 
      ); 
     $json_add_client = json_encode($this->data['add_client']); 

     // curl request start 
     $handle = curl_init(); 
     curl_setopt($handle, CURLOPT_URL, $url); 
     curl_setopt($handle, CURLOPT_POST, 1); 
     curl_setopt($handle,CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($handle, CURLOPT_POSTFIELDS,$json_add_client); 
     curl_setopt($handle, CURLOPT_FOLLOWLOCATION ,1); 
     curl_exec($handle); 
     // curl request end 

这里是卷曲响应

{ “错误”:真, “错误代码”:0, “errormessage的”: “所提供的数据流是无效的或空的”}

当我打印$ json_add_client变量数据看起来像这样

{“type”:2,“gender”:1,“name”:“Adnan Ahmad”,“language”:“en”,“number”:5,“address “:[{”street“:”Address“,”streetno“:”“,”code“:60000,”city“:”Multan“,”country“:”PK“}]}

我复制上述JSON数据并粘贴小发票测试API ..(这里是其中i粘贴链接)已经成功添加

http://www.smallinvoice.ch/api/testapi/clients

此时客户端。

我不知道为什么客户没有通过CURL请求添加。

如果有人知道那么请告诉我在哪里我错了。 我会感激你的。

回答

1

这可能是因为您需要设置POST请求来发送json而不是urlencoded数据。在curl_exec之前将此选项添加到您的卷发请求中。

curl_setopt($handle, CURLOPT_HTTPHEADER, 
    array(
     'Content-Type: application/json', 
     'Content-Length: ' . strlen($json_add_client) 
    ) 
); 

编辑: 他们的文档的进一步阅读之后,它看起来像你必须格式化您的POST数据遵循这种结构:

data={"amount":1,"value":1} 

在这种情况下,你会做同样的事情到:

$address[] = array(
    "street" => "Address", 
    "streetno" => "", 
    "code" => 60000, 
    "city" =>"Multan", 
    "country" => "PK" 
); 
$this->data['add_client'] = array(
    "type" => 2, 
    "gender" => 1, 
    "name" => "Adnan Ahmad", 
    "language" => "en", 
    "number" => 5, 
    "addresses" => $address 
); 
$json_add_client = json_encode($this->data['add_client']); 

// curl request start 
$handle = curl_init(); 
curl_setopt($handle, CURLOPT_URL, $url); 
curl_setopt($handle, CURLOPT_POST, 1); 
curl_setopt($handle,CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($handle, CURLOPT_POSTFIELDS, 
    array("data"=>$json_add_client) 
); 
curl_setopt($handle, CURLOPT_FOLLOWLOCATION ,1); 
curl_exec($handle); 
+0

先生非常感谢您的回复。我添加了上面发送的代码,但curl响应仍然相同。 –

+0

嗯。进一步查看他们的文档,你可能需要确保json字符串被分配到一个名为'data'的POST变量,如'data = {“amount”:1,“value”:1}'。 – patsweet

+0

先生,非常感谢你..这是工作..你很好..再次感谢..上帝保佑你 –