【问题标题】:HTPP request Ajax converted to Laravel for an external apiHTTP 请求 Ajax 转换为 Laravel 用于外部 api
【发布时间】:2020-05-12 18:15:21
【问题描述】:

我将尝试通过在 laravel 项目的控制器中创建的 api 将批量数据发送到另一个域

我的功能

public function test_prod_insert()
    {
       $usrl= url('/public/wheel_images/rohana_imgs');
        $users = DB::table('products')->select('id','title','sku','image1')->skip(0)->take(2)->get();
        echo"<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
        <script type='text/javascript' src='https://trk.mtrl.me/tracking.js?token=**********'></script>";

        foreach($users as $data){
    echo"<script>
        var model = {
  'time': 1518004715732,
  'token': '***************',
  'platform': 'laravel',
  'pluginVersion': '1.1.0',
  'params': {
    'categories': [
      '2'
    ],
    'id': '<?= $data->id ?>',
    'sku': '<?= $data->sku ?>',
    'imageUrl': '<?= $usrl ?>/<?= $data->image1 ?>',
    'name': '<?= $data->title ?>',
    'price': '',
    'url': '',
    'options': [

    ]
  }
};

$.ajax({
    type: 'POST',
    data: JSON.stringify(model),
    url: 'https://trk.mtrl.me/product',
    contentType: 'application/json'
}).done(function(res) {       
    console.log('res', res);
    // Do something with the result :)
});
</script>";
}
    }

现在我使用 Guzzle 转换为 Laravel,但我如何未能在 laravel 中发送这些数据显示 400 错误

在 larevel 中

 public function test_prod_insert()
        {
    echo"<script type='text/javascript' src='https://trk.mtrl.me/tracking.js?token=*********'></script>";
            $client = new Client();
            $res = $client->request('POST', 'https://trk.mtrl.me/product', [
      'form_params' => [
      'time'=> 1518004715732,
      'token'=> '*********',
      'platform'=> 'laravel',
      'pluginVersion'=> '1.1.0',
                ]
            ]);
            echo $res->getStatusCode();
            // 200
            echo $res->getHeader('content-type');
            // 'application/json; charset=utf8'
            echo $res->getBody();
            // {"type":"User"...'
    }

我如何在 laravel 中发送 id、sku、imgeurl... 请帮助我是 api 和 lar 的新手

【问题讨论】:

  • 你应该为客户端设置标题。 $headers = [ 'content-type' => 'application/x-www-form-urlencoded, 'accept' => 'application/json, text/plain, /' ]; $client = new Client([ 'headers' => $headers ]);
  • @berkaykılıç 如何在本节中添加循环

标签: laravel api http


【解决方案1】:

只需删除脚本并将数据发送到正文

  $client = new Client();
  $param=[
    "id"=> "91",
    "sku"=> "CM01-R",
    "imageUrl"=> "http://shop-tavan.customerboard.co/wp-content/uploads/2018/02/91.jpg",
    "name"=> "Rare unique blue shirt",
    "price"=> 23.65,
    "url"=> "http://shop-tavan.customerboard.co/product/rare-unique-blue-shirt/"
       ];
 $time = Carbon::now()->timestamp;
          $body=[
                "time"=>$time,
                "token"=> "***********",
                "platform"=>"Laravel",
                "pluginVersion"=> "1.1.0",
                "params"=>$param
              ];

$result=$client->request('POST', 'https://trk.mtrl.me/product', ['body'=>json_encode($body)]);
 echo $result->getStatusCode();

我将对 json 数据发送正文部分进行编码

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 2016-02-12
    • 2021-01-09
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多