dongqidi2799
2014-04-08 16:29
浏览 67
已采纳

小发票api curl请求

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 i m mistaking . i'll be grateful to you .

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanli12176 2014-04-08 16:39
    已采纳

    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);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题