在命令 window 中为 Aria 项目使用 curl 我得到了我想要的但在 PHP 文件中 return 空字符串

Using curl for Aria project in command window I get what I want but in PHP file it return empty string

当我在 git 中键入此命令时 bash:

curl -i -X POST --header "content-type: application/json;charset=UTF-8" --header "Authorization: Bearer X" -d @RequestBody.json https://app.studio.arria.com:443/alite_content_generation_webapp/text/X

我得到:

HTTP/2 200 date: Thu, 22 Nov 2018 09:45:27 GMT content-type: application/json;charset=UTF-8 server: nginx

[{"errorType":null,"errorMessage":null,"warnings":[],"result":"

Victoria has a land mass of 227,416 square kilometers and a population in 2016 of 5,938,100, which is 7.04% up on 2010's figure of 5,547,500.  The capital city, Melbourne, has a population of 4,353,514, which is 10.11% up on 2010's count of 3,953,939, and represents 73.31% of the state's population.

The premier, Daniel Andrews (ALP), is in his first term.


","wordCount":60},{"errorType":null,"errorMessage":null,"warnings":[],"result":"

The Australian Capital Territory has a land mass of 2,358 square kilometers and a population in 2016 of 390,800, which is 8.89% up on 2010's figure of 358,900.  The capital city, Canberra, has a population of 424,666, which is 6.58% up on 2010's count of 398,430, and represents 73.31% of the state's population.

The premier, Andrew Barr (ALP), is in his first term.


","wordCount":63},{"errorType":null,"errorMessage":null,"warnings":[],"result":"

The Northern Territory has a land mass of 1,349,129 square kilometers and a population in 2016 of 244,600, which is 6.49% up on 2010's figure of 229,700.  The capital city, Darwin, has a population of 123,396, which is 9.21% up on 2010's count of 112,987, and represents 73.31% of the state's population.

The premier, Michael Gunner (ALP), is in his first term.


","wordCount":62}]

但是当我使用 PHP 文件时,我在其中粘贴了从 Curl to PHP

获得的命令
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://app.studio.arria.com:443/alite_content_generation_webapp/text/X");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
    "file" => "@" .realpath("RequestBody.json")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Authorization: Bearer X";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}else {
    var_dump($result);
    $json = json_decode($result,true);
    var_dump($json);
}
curl_close ($ch);

var_dump($result) returns

string(0) ""

var_dump($json)returns

NULL

为此 post,我将 url 中项目的授权持有人和 ID 更改为 X,但如果您认为问题可能出在这些方面,我可以 post原始命令。

RequestBody.json:

{
    "data": [
      {
        "id": "Primary",
        "type": "1d",
        "dataSet": [
          ["State", "Premier", "Party", "Terms", "StateLandArea", "StatePop2010", "StatePop2016", "Capital", "CapitalPop2010", "CapitalPop2016"],
          ["Victoria", "Daniel Andrews", "ALP", "1", "227,416", "5,547,500", "5,938,100", "Melbourne", "3,953,939", "4,353,514"],
          ["the Australian Capital Territory", "Andrew Barr", "ALP", "1", "2,358", "358,900", "390,800", "Canberra", "398,430", "424,666"],
          ["the Northern Territory", "Michael Gunner", "ALP", "1", "1,349,129", "229,700", "244,600", "Darwin", "112,987", "123,396"]
        ]
      }
    ],
    "options": {
      "nullValueBehaviour": "SHOW_IDENTIFIER",
      "contentOutputFormat": "HTML"
    }
  } 

不要将 @ 方法与 php 的 libcurl 包装器一起使用,它在 PHP 5.5 中已弃用,在 PHP 5.6 中默认禁用,并已完全删除在 PHP 7.0 中。如果您使用的是 PHP 5.5 或更新版本,请改用 CURLFile,

$post = array(
    "file" => new CURLFile("RequestBody.json")
);

但这不是您的 php 代码不起作用的原因,当您在 php 中给 CURLOPT_POSTFIELDS 一个数组时,curl 将在 post 中创建您的请求multipart/form-data 格式,而您的 curl cli 调用将发送 json 编码的数据。要让 php-curl 像您的 cli 调用一样在 post 正文中发送 json,请将原始 json 字符串直接提供给 CURLOPT_POSTFIELDS,或者如果您已经在文件中有了响应,请使用 CURLOPT_INFILE、

这应该是 PHP 中的等效请求:

$ch = curl_init ();
$fp = fopen ( "RequestBody.json", "rb" );
curl_setopt_array ( $ch, array (
        CURLOPT_POST => 1,
        CURLOPT_INFILE => $fp,
        CURLOPT_HTTPHEADER => array (
                "content-type: application/json;charset=UTF-8",
                "Authorization: Bearer X" 
        ),
        CURLOPT_URL => 'https://app.studio.arria.com:443/alite_content_generation_webapp/text/X' 
) );
curl_exec ( $ch );
curl_close ( $ch );
fclose ( $fp );

或者如果您的请求正文是 dynamic/mutable,您可以这样做:

$post=array (
        'data' => array (

                array (
                        'id' => 'Primary',
                        'type' => '1d',
                        'dataSet' => array (

                                array (
                                        'State',
                                        'Premier',
                                        'Party',
                                        'Terms',
                                        'StateLandArea',
                                        'StatePop2010',
                                        'StatePop2016',
                                        'Capital',
                                        'CapitalPop2010',
                                        'CapitalPop2016' 
                                ),

                                array (
                                        'Victoria',
                                        'Daniel Andrews',
                                        'ALP',
                                        '1',
                                        '227,416',
                                        '5,547,500',
                                        '5,938,100',
                                        'Melbourne',
                                        '3,953,939',
                                        '4,353,514' 
                                ),

                                array (
                                        'the Australian Capital Territory',
                                        'Andrew Barr',
                                        'ALP',
                                        '1',
                                        '2,358',
                                        '358,900',
                                        '390,800',
                                        'Canberra',
                                        '398,430',
                                        '424,666' 
                                ),

                                array (
                                        'the Northern Territory',
                                        'Michael Gunner',
                                        'ALP',
                                        '1',
                                        '1,349,129',
                                        '229,700',
                                        '244,600',
                                        'Darwin',
                                        '112,987',
                                        '123,396' 
                                ) 
                        ) 
                ) 
        ),
        'options' => array (
                'nullValueBehaviour' => 'SHOW_IDENTIFIER',
                'contentOutputFormat' => 'HTML' 
        ) 
);

然后:

curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($post,JSON_PRETTY_PRINT));

而不是使用 CURLOPT_INFILE。