无法解决在使用PHP发布到Microsoft Business Central时出现的错误请求错误。

huangapple go评论57阅读模式
英文:

Unable to solve bad request error when trying to post to Microsoft Business Central using PHP

问题

我试图在Business Central中使用我的网站创建一个新客户。在使用Postman时,我能够成功完成,没有任何错误。但是,当我尝试使用PHP运行POST请求时,我收到了400坏请求错误。由于我对PHP不太熟悉,希望有一些明显的问题我可能忽略了。

我的代码:

$url = "https://api.businesscentral.dynamics.com/v2.0/" . $tennantId . "/Production/api/v2.0/customers";

$content = array(
    "displayName" => $obj->displayName,
    "addressLine1" => $obj->addressLine1,
    "addressLine2" => $obj->addressLine2,
    "city" => $obj->city,
    "state" => $obj->state,
    "country" => $obj->country,
    "postalCode" => $obj->postalCode,
    "email" => $obj->email,
    "currencyCode" => $obj->currencyCode
);

$options = array(
    "http" => array(
        "header" => "Content-Type: application/json\r\n" .
        "Authorization: Bearer " . $accessToken . "\r\n",
        "method" => "POST",
        "content" => json_encode($content)
    )
);

$context = stream_context_create($options);
$json = file_get_contents($url, false, $context);

$data = json_decode($json, true);

编辑:

我尝试使用cURL发送POST请求,没有收到任何错误。请求返回状态码200,但在Business Central中未创建客户。我将尝试将我的请求与Postman发送的请求进行比较,以查看是否遗漏了什么。

$ch = curl_init($url);
# 设置请求为使用POST发送JSON。
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $accessToken));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
# 返回响应而不是打印。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
# 发送请求。
$data = curl_exec($ch);
if (curl_errno($ch)) {
  $error_msg = curl_error($ch);
}
curl_close($ch);

// 处理$data响应
var_dump($data);
英文:

I am trying to create a new customer in business central with my website. I am able to do it using Postman without any errors. When I try to run a post request using PHP I get a 400 bad request error. I am not that familiar with PHP so I am hoping there is something obvious that I am missing.

My code:

$url = "https://api.businesscentral.dynamics.com/v2.0/ ". $tennantId ."/Production/api/v2.0/customers";

        $content = array(
            "displayName" => $obj->displayName,
            "addressLine1" => $obj->addressLine1,
            "addressLine2" => $obj->addressLine2,
            "city" => $obj->city,
            "state" => $obj->state,
            "country" => $obj->country,
            "postalCode" => $obj->postalCode,
            "email" => $obj->email,
            "currencyCode" => $obj->currencyCode
        );

        $options = array(
            "http" => array(
                "header" => "Content-Type: application/json\r\n" .
                "Authorization: Bearer " . $accessToken . "\r\n",
                "method" => "POST",
                "content" => json_encode($content)
            )
        );

        $context = stream_context_create($options);
        $json = file_get_contents($url, false, $context);

        $data = json_decode($json, true);

Edit:

I tried using cURL to send the POST request and I don't get any errors. The request return a 200 but the customer isn't created in the Business Central. I shall try to compare my request with the one Postman sends to see if I am missing something.

$ch = curl_init($url);
# Setup request to send json via POST.
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $accessToken));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
# Send request.
$data = curl_exec($ch);
if (curl_errno($ch)) {
  $error_msg = curl_error($ch);
}
curl_close($ch);

// Do something with the $data response
var_dump($data);

答案1

得分: 1

以下是已翻译的内容:

我注意到的第一件事是,在你的URL中缺少了companies部分。

这意味着Business Central不知道应该在哪家公司中创建客户。

正确的URL形式是:

$url = "https://api.businesscentral.dynamics.com/v2.0/" . $tennantId . "/Production/api/v2.0/companies([Company GUID])/customers";

要获取公司的GUID,你可以调用companies端点以获取可用公司的列表,其URL如下:

$url = "https://api.businesscentral.dynamics.com/v2.0/" . $tennantId . "/Production/api/v2.0/companies";

请注意,代码部分未进行翻译。

英文:

The first thing I notice is that you are missing the companies part in your URL.

This means that Business Central does not know which company the customer should be created in.

The correct form of the URL is:

$url = "https://api.businesscentral.dynamics.com/v2.0/". $tennantId ."/Production/api/v2.0/companies([Company GUID])/customers";

To get the Company GUID you can call the companies endpoint to get the list of available companies which would be this URL:

$url = "https://api.businesscentral.dynamics.com/v2.0/". $tennantId ."/Production/api/v2.0/companies";

答案2

得分: 0

感谢您的建议!我能够查看Postman生成的代码(我之前不知道这是可能的),并将其与我的代码进行比较。现在我知道问题是因为国家和电子邮件字段。这需要一些疑难解答,但我应该能够自己解决。感谢大家的建议!

Postman生成的代码并进行编辑以适应我的代码:

        $content = array(
            "displayName" => $obj->displayName,
            "addressLine1" => $obj->addressLine1,
            "addressLine2" => $obj->addressLine2,
            "city" => $obj->city,
            "state" => $obj->state,
            // "country" => $obj->country,
            "postalCode" => $obj->postalCode,
            // "email" => $obj->email,
            "currencyCode" => $obj->currencyCode
        );

        $curl = curl_init();

        curl_setopt_array(
            $curl,
            array(
                CURLOPT_URL => "https://api.businesscentral.dynamics.com/v2.0/" . $tennantId . "/Production/api/v2.0/customers",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($content),
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "Authorization: Bearer " . $accessToken,
                ),
            )
        );

        $response = curl_exec($curl);

        curl_close($curl);

        // 对$response响应进行处理
        var_dump($response);
英文:

Thank you for your suggestions! I was able to look at the code that Postman generated (I did not know this was possible) and compared it to my own. Now I know that the problem is because of the country and email fields. This will require a bit of troubleshooting but I should be able to do this on my own. Thank you to everyone for your suggestions!

Code generated by Postman and edited to fit my code:

        $content = array(
            "displayName" => $obj->displayName,
            "addressLine1" => $obj->addressLine1,
            "addressLine2" => $obj->addressLine2,
            "city" => $obj->city,
            "state" => $obj->state,
            // "country" => $obj->country,
            "postalCode" => $obj->postalCode,
            // "email" => $obj->email,
            "currencyCode" => $obj->currencyCode
        );

        $curl = curl_init();

        curl_setopt_array(
            $curl,
            array(
                CURLOPT_URL => "https://api.businesscentral.dynamics.com/v2.0/" . $tennantId . "/Production/api/v2.0/customers",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($content),
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "Authorization: Bearer " . $accessToken,
                ),
            )
        );

        $response = curl_exec($curl);

        curl_close($curl);

        // Do something with the $data response
        var_dump($response);

huangapple
  • 本文由 发表于 2023年5月22日 17:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定