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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧