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',
        ),
      ),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器