[英]small invoice api curl request
我正在使用SmallInvoice 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
這是卷曲反應
{“錯誤”:true,“錯誤代碼”:0,“錯誤消息”:“提供的數據流無效或為空”}
當我打印$ json_add_client變量數據時,看起來像這樣
{“ type”:2,“ gender”:1,“ name”:“ Adnan Ahmad”,“ language”:“ en”,“ number”:5,“ addresses”:[{“ street”:“ Address”, “streetno”: “”, “密碼”:60000, “城市”: “木爾坦”, “國”: “PK”}]}
我復制了上面的json數據並粘貼了小發票測試api ..(這是我粘貼的鏈接)
http://www.smallinvoice.ch/api/testapi/clients
此時已成功添加客戶端。
我不知道為什么客戶沒有通過CURL請求。
如果有人知道的話請告訴我哪里誤會了。 我會感激你的。
發生這種情況的可能是因為您需要設置POST請求來發送json而不是未編碼的數據。 在curl_exec
之前,將此選項添加到您的curl請求中。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.