2014-04-08 66 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

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