duanmajing9332 2018-10-11 10:38
浏览 95
已采纳

PHP,cURL,REST_API,JSON - 使用方法“PUT”在数据字段中插入值

I use the following code to insert values via API into certain fields of a database. The Method I have to use is PUT.

<?ph
header('Content-type: text/html; charset=utf-8');
error_reporting(E_ALL);
//ini_set('display_errors', '1');
$id = $_POST["id"];             
// URL to fetch
$url = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/42/elements/2155984";
// Setting the HTTP Request Headers
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';

$request_headers = array('Contect-Type:text/xml', 'Accept:text/xml');
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Content-type: application/json';
$request_headers[] = 'Accept-Encoding:  gzip, deflate, identity';
$request_headers[] = 'X-picturemaxx-api-key: K3f4e7m8dalksjdwdkkk';
$request_headers[] = "Authorization: Bearer token";


$dataj = array("classification_element_name" => 'string');
$data_json = json_encode($dataj);

 $ch = curl_init($url);
 // Set the url
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);

// Execute
$result = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Closing
curl_close($ch);

if ($code == 200) {
$result = json_decode($result, true);
print_r($result);
} else {
echo 'error ' . $code;
}
?>

Then I get the response

{
"request": {
"date": "2018-10-11T11:47:10+02:00",
"uid": "20181011-114710-03640bb2dcf2616d7"
},

So far so good. But unfortunately the values are not entered in the desired database field.

A Example-JSON-Body from the API looks like the following

{
  "classification_element_parent_id": 0,
  "classification_element_matchcode": "string",
  "classification_element_foreignref": "string",
  "localized": {
    "en-us": {
      "classification_element_name": "string"
    },
    "de-de": {
      "classification_element_name": "string"
    }
  }
}

A part of the JSON-Code looks like this

enter image description here

So how can I for example pass a value in the string classification_element_name that's part of the array item/localized/de-de?

I've tried this array before, too ->

$dataj['item']['localized']['de-de'] => 'string';

$data_json = json_encode($dataj);

  • 写回答

1条回答 默认 最新

  • douye4051 2018-10-11 10:46
    关注

    You have to build an array like this:

    $dataj = array (
      'classification_element_parent_id' => 0,
      'classification_element_matchcode' => 'string',
      'classification_element_foreignref' => 'string',
      'localized' => 
      array (
        'en-us' => 
        array (
          'classification_element_name' => 'string',
        ),
        'de-de' => 
        array (
          'classification_element_name' => 'string',
        ),
      ),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)