PHP发布数据格式

时间:2015-08-11 17:02:07

标签: php json post modx-revolution eventbrite

根据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;
}

该帖子正在运行,但数据没有更新,而回复是原始数据集。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我明白了。这是一个在查询字符串之前缺少斜杠的问题。我还删除了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';