PHP cURL将媒体上传到Eventbrite

时间:2016-12-14 15:45:59

标签: php curl amazon-s3 eventbrite

我正在尝试使用php 5.6中的cURL通过Eventbrite API v3上传事件的徽标。

使用的代码是一个python端口

$MYTOKEN = "...";
$baseurl = "https://www.eventbriteapi.com/v3";
$endpoint = "media/upload";
$querystring    =   http_build_query(array("token"=>$MYTOKEN,"type"=>"image-event-logo"));
$url = $baseurl.'/'.$endpoint.'/?'.$querystring;
$options = array(
    CURLOPT_RETURNTRANSFER => true,     
    CURLOPT_HEADER         => false,    
    CURLOPT_FOLLOWLOCATION => true,     
    CURLOPT_ENCODING       => "",       
    CURLOPT_USERAGENT      => "spider", 
    CURLOPT_AUTOREFERER    => true,     
    CURLOPT_CONNECTTIMEOUT => 120,      
    CURLOPT_TIMEOUT        => 120,     
    CURLOPT_MAXREDIRS      => 10,       
    CURLOPT_SSL_VERIFYPEER => false     
);  

$ch             = curl_init( $url );
curl_setopt_array( $ch, $options);
$content        = curl_exec( $ch );
curl_close( $ch );
$response = json_decode($content);

现在回复包含:

object(stdClass)[274]
  public 'upload_method' => string 'POST' (length=4)
  public 'upload_token' => string '...=' (length=112)
  public 'upload_url' => string 'https://s3.amazonaws.com/eventbrite-uploader-incoming-prod/' (length=59)
  public 'upload_data' => 
    object(stdClass)[273]
      public 'AWSAccessKeyId' => string '...' (length=20)
      public 'bucket' => string 'eventbrite-uploader-incoming-prod' (length=33)
      public 'acl' => string 'private' (length=7)
      public 'key' => string '...' (length=32)
      public 'signature' => string '...=' (length=28)
      public 'policy' => string '...==' (length=224)
  public 'file_parameter_name' => string 'file' (length=4)

使用该数据,我尝试使用multipart / form-data进行上传:

$filename = 'somefile';
$file = file_get_contents($filename);
$url = $response->upload_url;
$postdata = (array)$response->upload_data;  
$postdata["file"] = $file;
$postdata["upload_token"] = $response->upload_token;
$multipart = array();
foreach ($postdata as $key => $value) {
    $multipart[] = array('name' => $key,'contents' => $value);
}
$options[CURLOPT_POST] =  true; 
$options[CURLOPT_SAFE_UPLOAD] = true;
$options[CURLOPT_POSTFIELDS] = $postdata;
$options[CURLOPT_HTTPHEADER] = array("Content-Type:multipart/form-data");
$ch      = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
curl_close( $ch );

// notify upload completeness to Eventbrite

$endpoint = "media/upload";
$content =          $this->curlpost($endpoint,array("upload_token"=>$response->upload_token));
$response = json_decode($content);

// the logoid for the event object itself 
$logoid = $response->id;

我检索到以下错误:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>InvalidArgument</Code>
    <Message>POST requires exactly one file upload per request.</Message>
    <ArgumentName>file</ArgumentName>
    <ArgumentValue>0</ArgumentValue>
    <RequestId>...</RequestId>
    <HostId>...</HostId>
</Error>

还有其他什么缺失吗?

编辑:感谢@hanshenrik的建议,上面的代码已得到纠正。

0 个答案:

没有答案