简体   繁体   English

PHP HTTP POST请求的JSON数组语法

[英]JSON Array Syntax for PHP HTTP POST Requests

I'm attempting to write a PHP script that will query an API that I have access to. 我正在尝试编写一个PHP脚本,该脚本将查询我有权访问的API。 This API requires a JSON body post request, with one of the criteria being 此API需要JSON正文发布请求,其中一项条件是

"Analyses": ["Phenotype"]

Note that there are no double quotes around the second part of that string. 请注意,该字符串的第二部分周围没有双引号。 When I try to pass this script below with Httpful, this script gives me an HTTP 500 result. 当我尝试通过Httpful在下面传递此脚本时,该脚本给了我HTTP 500结果。 Without the Analyses criteria, I get a proper response (albeit an error asking for the Analyses information) in a JSON format output. 没有Analyzes标准,我会以JSON格式输出得到正确的响应(尽管错误,要求提供Analyzes信息)。 I assume this has something to do with the formatting/syntax of the Analyses string, and I'm not sure what to do about it. 我认为这与Analyzes字符串的格式/语法有关,我不确定该怎么做。 If I use single quotes, I get the 500 error. 如果使用单引号,则会出现500错误。 If I leave the quotes out entirely it assumes it's an array and give me an object error "Object reference not set to an instance of an object". 如果我完全省略引号,则假定它是一个数组,并给我一个对象错误“对象引用未设置为对象的实例”。 I don't know how to pass that format without quotes around it. 我不知道如何传递没有引号的格式。

$response = \Httpful\Request::post($url)
        ->sendsJson()
        ->body('{"apiUserKey":"abcde",
                "apiUserId":"efghi",
                "Species":"9606",
                "Analyses": "["Phenotype"]"
                 }')
        ->send();

Thanks! 谢谢!

You're example is almost right, but you are including one set of double quotes to many. 您的例子几乎是正确的,但是您要在一组引号中使用双引号。 You are trying to pass an array of strings in the "Analysis" field. 您正在尝试在“分析”字段中传递字符串数组。 The array itself does not need to be in double quotes. 数组本身不需要用双引号引起来。

$response = \Httpful\Request::post($url)
    ->sendsJson()
    ->body('{"apiUserKey":"abcde",
            "apiUserId":"efghi",
            "Species":"9606",
            "Analyses": ["Phenotype"]
             }')
    ->send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM