【问题标题】:PHP Post Data FormattingPHP Post 数据格式化
【发布时间】:2015-11-04 00:47:30
【问题描述】:

根据Eventbrite API v3 documentation,提交数据的首选方式是 JSON。我正在尝试通过 ExtJS 网格简单的组织者数据进行更新。未处理更改。

解决方案在 MODX 中,updateFromGrid.class.php 如下所示:

class UpdateOrganizerFromGridProcessor extends modProcessor {

    public function initialize() {
        $data = $this->getProperty('data');
        if (empty($data)) return $this->modx->lexicon('invalid_data');
        $data = $this->modx->fromJSON($data);
        if (empty($data)) return $this->modx->lexicon('invalid_data');
        $this->id =  $data['id'];

        $this->params = array ();
        // build JSON content for form submission...cooking key names
        $this->formData = array (
          'organizer.name' =>  $data['name'],
          'organizer.description.html' => $data['description'],
          'organizer.logo.id' => $data['logo_id'],
        );
        $this->formJSON = $this->modx->toJSON($this->formData); 

        $this->args = array('id' => $this->id, 'params' => $this->params);
        return parent::initialize();
    }

    public function process() {
        // call to main class to save changes to the Eventbrite API
        $this->mgr_client = new Ebents($this->modx);
        $this->output = $this->mgr_client->postData('organizers', $this->args, $this->formJSON);
        $response = json_decode(json_encode($this->output), true);
        return $this->outputArray($response);
    }
 }
 return 'UpdateOrganizerFromGridProcessor';

上面的json输出是:

{"organizer.name":"Joe Organizer","organizer.description":"Joe is the Uberest Organizer."}

而我的发帖功能是:

//send data to Eventbrite
function postData($method, $args, $JSONdata) {
  error_log("JSON Payload : " . $JSONdata);
  // Get the URI we need.
  $uri = $this->build_uri($method, $args);
  // Construct the full URL.
  $request_url = $this->endpoint . $uri;
  // This array is used to authenticate our request.
  $options = array(
    'http' => array(
      'header'  => "Content-type: application/json\r\n"
                    . "Accept: application/json\r\n",
      'method'  => 'POST',
      'content' => $JSONdata,
      'header'  => "Authorization: Bearer " . $this->token
    )
  );
  // Call the URL and get the data.
  error_log("URL: " . $request_url);
  error_log("Content: " . $options['http']['content']);
  $resp = file_get_contents($request_url, false, stream_context_create($options));
  // parse our response
    if($resp){
        $resp = json_decode( $resp );

        if( isset( $resp->error ) && isset($resp->error->error_message) ){
           error_log( $resp->error->error_message );
        }
    }
    // Return it as arrays/objects.
    return $resp;
}

function build_uri($method, $args) {
  // Get variables from the $args.
  extract($args);
  // Get rid of the args array.
  unset($args);
  // Create an array of all the vars within this function scope.
  // This should be at most 'method', 'id' and 'data'.
  $vars = get_defined_vars();
  unset($vars['params']);
  // Put them together with a slash.
  $uri = implode($vars, '/');
    if (!empty($params)) {
      return $uri ."?". http_build_query($params);
    }
  return $uri;
}

帖子正在运行,但数据没有更新,回复的是原始数据集。我在这里错过了什么?

【问题讨论】:

  • 请发布更新文件或完整代码
  • @bicho 添加了 updateFromGrid 函数,该函数正在生成已经提供的 JSON。
  • 如果您进行手动更新会出现错误?
  • @bicho 如果你的意思是通过 api webtools 那么没有。通过他们的表格更新记录可以正常工作。我找不到有关部分更新的任何信息,但我假设如果我留下一个现有字段(如 organizer.logo.id)它应该提交。我无法想象我必须发送每个字段值来更新一个,但我在文档中找不到任何验证。

标签: php json post modx-revolution eventbrite


【解决方案1】:

我想通了。这是查询字符串之前缺少斜杠的问题。我还删除了 JSON 数据有效负载,并以表单编码形式提交。最后一课如下:

class UpdateOrganizerFromGridProcessor extends modProcessor {

  public function initialize() {
    $data = $this->getProperty('data');
    if (empty($data)) return $this->modx->lexicon('invalid_data');
    $data = $this->modx->fromJSON($data);
    if (empty($data)) return $this->modx->lexicon('invalid_data');

    $this->id =  $data['id'];

    $this->params = array (
        'organizer.name' =>  $data['name'],
        'organizer.description.html' => $data['description'],
        'organizer.logo.id' => $data['logo_id'],
    );

    $this->args = array('id' => $this->id, 'data'=> '', 'params' => $this->params);
    return parent::initialize();
  }

  public function process() {
      // call to main class to save changes to the Eventbrite API
      $this->mgr_client = new Ebents($this->modx);
      $this->output = $this->mgr_client->postData('organizers', $this->args);
      $response = json_decode(json_encode($this->output), true);
      return $this->outputArray($response);
    }
}
return 'UpdateOrganizerFromGridProcessor';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    相关资源
    最近更新 更多