尝试使用PHP发布到Microsoft Business Central时无法解决错误请求

hjzp0vay  于 2023-05-27  发布在  PHP
关注(0)|答案(2)|浏览(150)

我正试图创建一个新的客户在商业中心与我的网站。我可以用Postman来做,没有任何错误。当我尝试使用PHP运行post请求时,我得到了一个400 bad request错误。我不是很熟悉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);
# 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);
at0kjp5o

at0kjp5o1#

我注意到的第一件事是你的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";
vzgqcmou

vzgqcmou2#

谢谢你的建议!我能够查看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);

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

相关问题