繁体   English   中英

小发票api卷曲请求

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM