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

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动