POST呼叫无法正常工作?

时间:2015-04-14 08:47:01

标签: php html json rest post

我需要进行POST调用以在服务器上保存数据。服务器要求某些项目为JSON-Encoded List。但是,当我进行后调用并查看$result时," JSON编码列表的数据"未保存且项目为null。然而,POST呼叫是成功的。

$items = json_encode((array(
    "name" => $_GET['title'],
    "sub_type" => $subtype_fd,
    "calories" => intval($_GET['calories']),
    "carbohydrate" => floatval($_GET['carbohydrate']),
    "cholesterol" => floatval($_GET['cholesterol']),
    "fiber" => floatval($_GET['fiber']),
    "protein" => floatval($_GET['protein']),
    "saturated_fat" => floatval($_GET['saturated_fat']),
    "unsaturated_fat" => floatval($_GET['unsaturated_fat']),
    "sodium" => floatval($_GET['sodium']),
    "sugar" => floatval($_GET['sugar'])
)));

var_dump($items);

$data = array('note' => $_GET['title'], 'sub_type' => $subtype_bld, 'items' => $items);

$options = array(
    'http' => array(
        "header" => "Content-Type: application/x-www-form-urlencoded\r\nAuthorization: Bearer {$_COOKIE['access_token']}\r\n",
        'method' => 'POST',
        'content' => $data
    ),
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

来自API的信息

Host: jawbone.com 
Accept: application/json
Content-Type: multipart/form-data

1 个答案:

答案 0 :(得分:2)

content上下文选项应该是一个字符串,您要提供一个数组。它应该是URL编码格式的字符串。 http_build_str函数会将关联数组转换为此格式。所以使用:

'content' => http_build_str($data)