我正在为我的PHP项目编写单元测试,
单元测试是模拟php://input数据,
我读了这本手册,它说:
php:// input是一个只读流,允许您从请求正文中读取原始数据.
如何php://input在PHP中模拟或编写请求主体?
这是我的源代码和单元测试,两者都是简化的.
来源:
class Koru
{
static function build()
{
// This function will build an array from the php://input.
parse_str(file_get_contents('php://input'), $input);
return $input;
}
//...
Run Code Online (Sandbox Code Playgroud)
单元测试:
function testBuildInput()
{
// Trying to simulate the `php://input` data here.
// NOTICE: THIS WON'T WORK.
file_put_contents('php://input', 'test1=foobar&test2=helloWorld');
$data = Koru::build();
$this->assertEquals($data, ['test1' => 'foobar',
'test2' => 'helloWorld']);
}
Run Code Online (Sandbox Code Playgroud) 我读过了:
但我仍然不知道它是如何工作的以及它有什么用处.有人可以解释它是什么以及何时我想使用它?
作为个人编程项目,我正在努力抓取我的大学课程目录并将数据作为REST API提供.我已经成功地删除了所有数据并将其存储在数据库中,现在我正在使用API.
课程可以根据许多标准进行过滤:教师,大学,学分,时间,日等.
在这种情况下提供API的最佳方法是什么?
选项1
提供大量的URL,例如
example.com/api/byinstructor/<instructorcode>
example.come/api/bycollege/<collegecode>
example.com/api/bycollegeandinstructor/<collegecode>/<instructorcode>
...and so on
Run Code Online (Sandbox Code Playgroud)
我需要有一个所有排列的URL.对我和API消费者而言,这看起来非常麻烦,而且非常糟糕.
选项2
仅为主要选项提供API,例如:
example.com/api/byinstructor/<instructorcode>
example.come/api/bycollege/<collegecode>
Run Code Online (Sandbox Code Playgroud)
如果消费者想要的话bycollegeandinstructor,他会对他进行过滤.
选项3
用户将JSON字符串传递给我,我使用它来获取过滤条件
example.com/api/getcourses/<jsonstring>
jsonstring =
{
instructor:<instructorcode>,
college:<collegecode>,
...and so on
}
Run Code Online (Sandbox Code Playgroud)
我想而不是Json字符串,我也可能需要一个POST数组,但这对于消费者来说似乎是无本能的,因为他正在获取数据.
还是有其他方法这样做我不知道?如果它是第三个选项是最佳选项,您是否可以提供一个简短的摘要,以便根据可能具有可变数值的JSOn字符串准备SQL查询?
我正在尝试将我的Flexbox布局更改为新的display: grid,我有三列.
<div class="grid">
<div class="three wide column"></div>
<div class="two wide column"></div>
<div class="two wide column"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
是否可以指定一些列从右侧开始而不是从左侧开始,如下图所示?
(并且没有指定grid-column-start和grid-column-end,这可以通过margin-left: autoFlexbox完成)
<div class="grid">
<div class="three wide column"></div>
<div class="right floated two wide column"></div>
<div class="right floated two wide column"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
我正在开发一个具有微服务架构的网站,每个服务都拥有一个数据库.数据库存储微服务所需的数据.
Post,Video服务需要用户信息,所以两个服务都订阅了NEW_USER_EVENT.
的NEW_USER_EVENT时候,那里已经注册的新用户将被触发.
一旦服务收到NEW_USER_EVENT,他们就将传入的用户信息放到他们自己的每个数据库中.所以他们可以在不询问User服务的情况下做事.
到现在为止还挺好.但问题出现了:
也许我可以从现有服务中获取信息.但事件由消息队列(NSQ)推送.
如果我要从其中一个微服务中复制数据,我如何确定哪个服务具有最新的用户信息?(因为有些服务没有收到最新的活动)
阅读更多:
我试图让commentId问type = x及target = x,
通常(这是一个例子),表的结构应如下所示:
+-----------+-------+--------+
| commentId | type | target |
+-----------+-------+--------+
| 1 | post | 2 |
| 2 | post | 8 |
| 3 | video | 6 |
+-----------+-------+--------+
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可以使用此查询来获取commentId:
SELECT `commentId` FROM `comment_datas` WHERE type = 'post' AND target = '2'
Run Code Online (Sandbox Code Playgroud)
但这是表的真实结构(使用键值设计):
+-----------+--------+-------+
| commentId | name | value |
+-----------+--------+-------+
| 1 | type | post | …Run Code Online (Sandbox Code Playgroud) 是否有任何可能的方法来生成所有表情符号并使用JavaScript 将它们附加到单个选择下拉列表中?或者我必须手动输入每个?
<select>
<option></option>
<option></option>
<select>
<script>
function()
{
// How do I put all the emojis into the select dropdown..?
}
</script>
Run Code Online (Sandbox Code Playgroud) 我是 Golang 的新手,在参观了A Tour of Go 之后,我正在尝试制作自己的东西。
我想将不同类型的结构放入单个切片(或结构?),
所以我可以使用for循环将每个结构传递给函数。
在 PHP 中,我可以将我的类存储在一个数组中,并将它们中的每一个传递给foobar()这样的:
$classes = [$A, $B, $C, $D]; // $A, $B, $C, $D are classes (called `struct` in Golang).
foreach ($classes as $class)
foobar($class);
Run Code Online (Sandbox Code Playgroud)
我尝试在 Golang 中做同样的事情,我希望它看起来像这样:
A{B{}, C{}, D{}}
Run Code Online (Sandbox Code Playgroud)
由于我使用失败slice,我决定使用struct我的structs:
type A struct {
B
C
D
}
type B struct {
Date string
}
type C struct {
Date string
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试对来自以下位置的原始数据进行分组:
items:
[
{
category: "blog",
id : "586ba9f3a36b129f1336ed38",
content : "foo, bar!"
},
{
category: "blog",
id : "586ba9f3a36b129f1336ed3c",
content : "hello, world!"
},
{
category: "music",
id : "586ba9a6dfjb129f1332ldab",
content : "wow, shamwow!"
},
]
Run Code Online (Sandbox Code Playgroud)
到
[
{
category: "blog",
items:
[
{
id : "586ba9f3a36b129f1336ed38",
content : "foo, bar!"
},
{
id : "586ba9f3a36b129f1336ed3c",
content : "hello, world!"
},
]
},
{
category: "music",
items:
[
{
id : "586ba9a6dfjb129f1332ldab",
content : "wow, shamwow!"
}
]
} …Run Code Online (Sandbox Code Playgroud) javascript arrays object multidimensional-array categorization
通过使用path/filepath包含以下示例的包,您可以从文件路径获取完整目录路径.
package main
import (
"fmt"
"path/filepath"
)
func main() {
// Output: /path/to/dir
fmt.Println(filepath.Dir("/path//to/dir/file.ext"))
}
Run Code Online (Sandbox Code Playgroud)
但是有没有从路径中Parent获取的功能dir?(这是文件目录的名称):
// The `Parent` is what I want,
// and this is a pseudo-code example, this won't actually work.
//
// Output: dir
fmt.Println(filepath.Parent("/path//to/dir/file.ext"))
Run Code Online (Sandbox Code Playgroud)
如果无法使用函数完成,如何使用RegExp获取父级的名称?
package main
import (
"fmt"
)
func main() {
var square int
box := [4]int{1, -2, 3, 4}
square = box * *box
fmt.Println("The square of the first box is", square)
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我正确的方法吗?问题是square的直接无效(type [4] int)