Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Drupal7中读取JSON数据_Drupal - Fatal编程技术网

如何在Drupal7中读取JSON数据

如何在Drupal7中读取JSON数据,drupal,Drupal,我有一个服务器1,它将JSON数据发布到我的服务器2 URL(Drupal站点) 示例JSON请求对象 {"ConfirmationNumber":"344", "E-mailAddress":"EMAIL@ADDRESS.COM", "FirstName":"FIRSTNAME", "LastName":"LASTNAME", "SKU":"XYZ"} 我需要阅读Drupal站点中的上述请求意味着服务器2解析JSON对象并执行一些业务逻辑。非常感谢任何快速的帮助 例如: 一些外部站点正在将

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

示例JSON请求对象

{"ConfirmationNumber":"344", "E-mailAddress":"EMAIL@ADDRESS.COM", "FirstName":"FIRSTNAME", "LastName":"LASTNAME", "SKU":"XYZ"}
我需要阅读Drupal站点中的上述请求意味着服务器2解析JSON对象并执行一些业务逻辑。非常感谢任何快速的帮助

例如:

一些外部站点正在将JSON数据发布到我上面的URL


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

您应该使用drupal\u json\u encode:

注意。

使用drupal\u json\u解码($data)。您将得到一个php数组。然后使用foreach并在其中循环以获取值

/**
 * 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;
}
$content = drupal_json_decode($data);
foreach($content as $key => $value) {
  // here $value will contain your value. You can perform any business logic with     $value...
}
读取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  
}

如果需要从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'];

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