douchui7332 2018-10-12 09:00
浏览 96
已采纳

PHP,cURL,REST-API - 将具有许多值的变量放入JSON数组中

with the following code

<?php
                //PHP Infos anzeigen lassen
            //phpinfo();
          header('Content-type: text/html; charset=utf-8');
            // Inhaltstyp und Zeichenkodierung für Skript festlegen
                $User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';

            $id = $_POST["id"];             
                // Übernahme Formulareingabe
                $url = "http://hub.culturegraph.org/entityfacts/$id";
                // URL festlegen

                $request_headers[] = 'Accept: application/json';
              $request_headers[] = 'Content-Type: application/json; charset=utf-8';
              $request_headers[] = 'Accept-Encoding:    gzip, deflate, identity';
              $request_headers[] = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
              $request_headers[] = 'X-picturemaxx-api-key: key';
                $request_headers[] = "Authorization: Bearer token";
            // Optionale Anfrageoptimierungen

              $ch = curl_init($url);
              //  Initiate curl
        curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_ENCODING, "");
                // Anfrage optimieren
                // Execute
                $result = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
        curl_close($ch);
        // Closing

        $data = json_decode($result, true); // Dekodiert eine JSON-Zeichenkette, Übergibt an Variable
        foreach($data['variantName'] as $alternativ) {
        echo $alternativ . " ; ";

}

i receive json-data. The output from the variable $alternativ looks like

"Johann Wolfgang Goethe ; Johan Wolfgang von Goethe ; Johan Wolphgang Goethe ; Johan W. von Goethe ; Joh. Wolfg. v. Goethe ; J. Wolfgang Goethe ; J. W. v. Goethe ; J. W. Goethe ; Jan Wolfgang Goethe ; Jean Wolfgang von Goethe ; Juan Wolfgang von Goethe ; Juan Wolfgang Goethe ; Juan W. Goethe ; João Wolfgang von Goethe ; Iohann Wolfgang Goethe ; Iohan Wolphgang Goethe ; Ioannes W. Goethe ; I. W. Goethe ; Wolfgango Goethe ; Wolfango Goethe ; W. von Goethe ; Volfgango Goethe ; Volfango Goethe ; Giov. L. Goethe ; G. L. Goethe ; Goethe ;

and so on.

Next i try is to PUT the values of the variable $alternativ into the json-array/database field 'classification_element_name'

$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/42/elements/2156013";



$dataj = array (
  'classification_element_parent_id' => 0,
  'classification_element_matchcode' => '',
  'classification_element_foreignref' => '',
  'localized' => 
  array (
    'en-us' => 
    array (
      'classification_element_name' => '',
    ),
    'de-de' => 
    array (
      'classification_element_name' => $alternativ,
    ),
  ),
);

        $data_json = json_encode($dataj);

              $ch = curl_init($url2);
              // Set the url
        curl_setopt( $ch, CURLOPT_URL, $url2 );
        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
                $result2 = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
                $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                // Closing
        curl_close($ch);

This works. But only one respectively the last value of the originally many values from the variable $alternativ appears in the database field. How do I change the code so that all values of the variable $alternativ are transferred into the database field?

  • 写回答

1条回答 默认 最新

  • dongluo3331 2018-10-12 12:28
    关注

    you just loop & echo the elements and dont assign them to a variable

    foreach($data['variantName'] as $alternativ) {
        echo $alternativ . " ; ";
    }
    

    $alternativ will allways be the last element of $data['variantName']. You need to replace $alternativ with $data['variantName'] in your $dataj array or assign a new Variable and use this in your $dataj array

    eg:

    $alternativData = array();
    foreach($data['variantName'] as $alternativ) {
        echo $alternativ . " ; ";
        $alternativData[] = $alternativ;
    }
    
    $dataj = array (
      'classification_element_parent_id' => 0,
      'classification_element_matchcode' => '',
      'classification_element_foreignref' => '',
      'localized' => 
      array (
        'en-us' => 
        array (
          'classification_element_name' => '',
        ),
        'de-de' => 
        array (
          'classification_element_name' => $alternativData,
        ),
      ),
    );
    

    or if it should look like 'data1;data2;data3..' them use:

    $alternativData = array();
    foreach($data['variantName'] as $alternativ) {
        echo $alternativ . " ; ";
        $alternativData[] = $alternativ;
    }
    
    $dataj = array (
      'classification_element_parent_id' => 0,
      'classification_element_matchcode' => '',
      'classification_element_foreignref' => '',
      'localized' => 
      array (
        'en-us' => 
        array (
          'classification_element_name' => '',
        ),
        'de-de' => 
        array (
          'classification_element_name' => implode(';', $alternativData),
        ),
      ),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置