在终端中使用cURL发布数组 [英] Posting array using cURL in Terminal

查看:56
本文介绍了在终端中使用cURL发布数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为应用构建Web服务,因此数据存储在我拥有的在线数据库中.我目前正在构建php文档,并且对如何在 Terminal Mac应用程序中使用cURL POSTarray感到好奇.

I'm trying to build a webservice for an app, so the data stores in an online database I have. I'm currently building the php document, and I'm curious as to how I can POST an array using cURL in the Terminal Mac app.

您会看到,应用程序将向Web服务发送一个由字符串组成的数组.例如这样的["String 1", "String 2", "String 3"]. 但是要测试我将这些值插入数据库的代码是否有效,我需要以某种方式将此数据传递给Web服务.到目前为止,我一直在使用 Mac Terminal应用程序,并以curl -F "user_id=1" localhost/test/webservice.php"这样的形式发布,但我不知道如何传递数组.

You see, the application will send an array consisting of strings to the webservice. For example something like this ["String 1", "String 2", "String 3"]. But to test if the code I have for inserting these values into the database works, I need to pass this data to the webservice somehow. Up until now I've been using the Mac Terminal app and posted like this curl -F "user_id=1" localhost/test/webservice.php", but I don't know how to pass an array.

到目前为止,我的情况是这样:

What I have so far is this:

curl -F "user_id=1" -F "title=Random title 123" -F "num_items=2" -F "items[]=[Item 1, Item 2]" localhost/test/webservice.php

但是我不认为这是传递数组的正确方法,因为我认为我在php文档中拥有的代码是正确的,但是存储在数据库中的值实际上是 array .我相信items[]是启动它的正确方法,但是我不确定=符号后会出现什么.

But I don't think that's the correct way to pass an array, because I believe the code I have in my php document is correct, but the value that's stored in my database is literally the word array. I believe items[] is the correct way to start it, but I'm not so sure about what comes after the =-sign.

有人知道这样做的正确方法吗?

Anyone know the correct way to do this?

如果这是正确的,则我将数据存储在数组中的方式有​​问题.我正在使用stmtbind_param()关键字,如下所示:

If this happens to be correct, then there's something wrong with how I store the data in my array. I'm using stmt and the bind_param() keyword like this:

$items = $_POST["items"]; // This is the array

$stmt = $this->db->prepare('INSERT INTO items (item_id, user_id, title) VALUES (?, ?, ?)');
$stmt->bind_param('iis', $item_id, $user_id, $items);
$stmt->execute();
$stmt->close();

我想要实现的基本上是使用一个insert语句将数组中的所有值发布到数据库中.如果那是错误的,那么正确的方法是什么?

What I'm trying to achieve is basically posting all the values fron the array into the database with one insert statement. If that's wrong, what is the correct way to do it?

感谢所有帮助!

谢谢.

推荐答案

首先请确保您要执行-F(对于multipart/form-data)或-d(对于application/x-www-form-urlencoded).无论哪种情况,这都是传递数组的方法:

First make sure you want to do -F(for multipart/form-data) or the -d(for application/x-www-form-urlencoded). In either case, here is how you should pass the array:

-F "items[]=Item 1" -F "items[]=Item 2"

或使用-d

-d "items[]=Item 1" -d "items[]=Item 2"

您的服务器将接收以下格式的数据:

Your server will receive the data as following format:

[items] => Array
    (
        [0] => Item 1
        [1] => Item 2
    )

这篇关于在终端中使用cURL发布数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆