【问题标题】:easybill REST API POST over PHPeasybill REST API POST 通过 PHP
【发布时间】:2020-12-17 07:33:23
【问题描述】:

这是我在这里的第一篇文章。我希望我的问题是正确的。 我使用谷歌翻译从德语到英语,因为我的英语不是那么好。

我想使用easybill 的“api”。得到工作。但不幸的是,我无法使用 POST。

我希望有人可以帮助我。

我想创建一个新客户进行测试。我的目标是稍后使用 PHP 生成发票。 easybill 的“api”可以在这里找到:https://www.easybill.de/api/

我尝试了以下代码,但不幸的是我找不到错误。

$url = "https://api.easybill.de/rest/v1/customers";
$data =  array(
    
          'first_name'   => 'Foo',
          'last_name'    => 'Bar',
          'company_name' => 'FooBar GmbH',
          'emails'       => array ('foo.bar@foobar.com'
            )


);

$postdata = json_encode(array($data));
$accesstoken = "XXXXXXXXXXX";

$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: Bearer '.$accesstoken;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
    $result = curl_exec($ch);
    curl_close($ch);
    print_r ($result);

来自德国的问候。

【问题讨论】:

  • 您尝试 POST 时的确切错误是什么?
  • 400 错误请求 nginx
  • Content-length: 0 肯定不对。
  • 谢谢。现在我更进一步了。 :-) 我注释掉了这一行。现在出现错误消息: {"code":20000,"message":"Bitte f\u00fcllen Sie alle Felder aus.","arguments":["last_name","company_name"]} 但数据已填写。

标签: php json rest


【解决方案1】:

你的错误:

  1. Content-length: 0 没有任何意义,在这里完全错误。 Afaik 您可以完全删除它,因为 curl 会为您执行此操作。

  2. $postdata = json_encode(array($data)); 会将您的数组包装在另一个数组中,这在此处是不正确的。它产生于:

    [{"first_name":"Foo","last_name":"Bar","company_name":"FooBar GmbH","emails":["foo.bar@foobar.com"]}]

它必须在哪里:

{"first_name":"Foo","last_name":"Bar","company_name":"FooBar GmbH","emails":["foo.bar@foobar.com"]}

只需删除数组调用:

$postdata = json_encode($data);

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2019-01-05
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多