【问题标题】:How to read JSON data in Drupal 7如何在 Drupal 7 中读取 JSON 数据
【发布时间】:2013-11-19 10:24:44
【问题描述】:

我有一个服务器 1,它将 JSON 数据发布到我的服务器 2 URL(Drupal 站点)

示例 JSON 请求对象

{"ConfirmationNumber":"344", "E-mailAddress":"EMAIL@ADDRESS.COM", "FirstName":"FIRSTNAME", "LastName":"LASTNAME", "SKU":"XYZ"}

我需要阅读 Drupal 站点中的上述请求意味着服务器 2 来解析 JSON 对象并执行一些业务逻辑。非常感谢任何快速帮助

示例: http://mydrupalsite.com/services/register

一些外部网站将 JSON 数据发布到我上面的 URL。

我需要阅读他们在我的 drupal 网站上发布的 JSON 内容。这就是我要的。获取/读取数据是我的第一步。解析很好,我们可以在下一步进行。

【问题讨论】:

    标签: drupal


    【解决方案1】:

    读取 json 响应。

    $url = "your request url";
    $request = drupal_http_request($url);
    $json_response = drupal_json_decode($request->data);
    foreach ($json_response as $response_data) {
      //your operation code  
    }
    

    【讨论】:

      【解决方案2】:
      /**
       * Implementation of hook_services_resources().
       */
      function yourmodulename_services_resources() {
        return array(
         'yourmodulename' => array(
           'create' => array(
             'help' => 'Retrievs a test',
             'callback' => '_yourmodulename_create',
             'access callback' => '_yourmodulename_access',      
             'args' => array(
               array(
                  'name' => 'data',
                  'type' => 'struct',
                  'description' => 'The note object',
                  'source' => 'data',
                  'optional' => FALSE, 
               ),
             ),
           ),
         ),
        );
      }
      
      /**
       * Callback for creating  resources.
       *
       * @param object $data
       * @return object
       */
      function _yourmodulename_create($data) {
      return $data;
      }
      
      /**
       * Access callback for the note resource.
       *
       * @param string $op
       *  The operation that's going to be performed.
       * @param array $args
       *  The arguments that will be passed to the callback.
       * @return bool
       *  Whether access is given or not.
       */
      function _yourmodulename_access($op, $args) {
        $access = TRUE;
        return $access;
      }
      

      【讨论】:

        【解决方案3】:

        【讨论】:

        • 因为我是 drupal 新手。你能提供它的语法吗?谢谢你快速的回复。基本上我不想在 drupal 中解析 JSON。相反,我想知道如何在 drupal 中读取发布到我们 URL 的 JSON 数据。
        • 马里奥:我更新了问题,请检查我的示例以获得我想要的。感谢您的回复
        【解决方案4】:

        使用 drupal_json_decode($data)。你会得到一个 php 数组。然后使用 foreach 并循环遍历它以获取值。

        $content = drupal_json_decode($data);
        foreach($content as $key => $value) {
          // here $value will contain your value. You can perform any business logic with     $value...
        }
        

        【讨论】:

          【解决方案5】:

          如果需要从 url 中获取 JSON 对象,例如 {"text":"HelloWorld"}。我建议你使用这种方法:

          //Create a path
          $mypath = 'http://example.com';
          //Create a option
          $options = array('query' => array('parametr1' => '1', 'parametr2' => '2'));
          //Create url
          $url = url($path = $url, $options);
          //Create request 
          $request = drupal_http_request($url);
          //Take response data(A string containing the response body that was received)
          $json_response = drupal_json_decode($request->data);
          // Varible text return HelloWorld 
          $text = $json_response['text'];
          

          问候

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多