【问题标题】:Making POST request in Wordpress to externa API在 Wordpress 中向外部 API 发出 POST 请求
【发布时间】:2021-07-10 05:29:51
【问题描述】:

我正在尝试使用 Wordpress(也在 localhost 中)向我的本地主机中的 API 发出 POST 请求。如果我在 Postman 中尝试正文,一切正常,但在 Wordpress 中我收到此错误:

Response:
Array
(
    [headers] => Requests_Utility_CaseInsensitiveDictionary Object
        (
            [data:protected] => Array
                (
                    [host] => localhost:8000
                    [date] => Array
                        (
                            [0] => Thu, 15 Apr 2021 07:19:50 GMT
                            [1] => Thu, 15 Apr 2021 07:19:50 GMT
                        )

                    [x-powered-by] => PHP/7.3.24-(to be removed in future macOS)
                    [cache-control] => no-cache, private
                    [content-type] => application/json
                    [x-ratelimit-limit] => 60
                    [x-ratelimit-remaining] => 59
                    [access-control-allow-origin] => *
                )

        )

    [body] => {
    "message": "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'type' cannot be null (SQL: insert into `items` (`type`, `name`, `date`, `description`, `poster`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, 2021-04-15 07:19:50, 2021-04-15 07:19:50))",

这是请求:

$url = 'http://localhost:8000/api/item/store';

$item = array(
  "type"=>"type",
  "name"=>"blablabla",
  "description"=>"blablabla",
  "date"=>"2021-05-14",
  "poster"=>"poster"
);

$args = array(
    'headers' => array(
        'Content-Type' => 'application/json'
    ),
    'body'=> wp_json_encode($item),
    );

$response = wp_remote_post($url, $args);
 
if ( is_wp_error( $response ) ) {
  $error_message = $response->get_error_message();
  echo "Something went wrong: $error_message";
} else {
  echo 'Response:<pre>';
  print_r( $response );
  echo '</pre>';
}

【问题讨论】:

    标签: wordpress laravel api post request


    【解决方案1】:

    通过编辑 $item 来解决。我发送的格式不正确

    $item = array("item"=>[
      "type"=>"sport",
      "name"=>"funziona",
      "description"=>"film di prova harcoded",
      "date"=>"2021-05-14",
      "poster"=>"poster"
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-06
      • 2017-05-24
      • 2020-05-23
      • 2019-07-07
      • 1970-01-01
      • 2022-01-25
      • 2016-12-15
      • 1970-01-01
      相关资源
      最近更新 更多