简体   繁体   中英

small invoice api curl request

i am working on SmallInvoice api . here is the link

smallinvoice.ch/api/general/overview

i am trying to add a client in small invoice account . here is the code

$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

here is the curl response

{"error":true,"errorcode":0,"errormessage":"The datastream provided is not valid or empty"}

when i print $json_add_client variable data looks like this

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

i copied above json data and paste small invoice test api .. (here is the link where i paste)

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

At this time client has been added successfully .

I DON'T KNOW WHY CLIENT IS NOT ADDING THROUGH CURL REQUEST .

if any body knows then please tell me where im mistaking . i'll be grateful to you .

This is likely occurring because you need to setup the POST request to send json instead of urlencoded data. Add this option to your curl request right before curl_exec .

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

EDIT: After further reading of their documentation, it looks like you have to format your POST data to follow this structure:

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

In this case, you would do something similar to:

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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