2010-09-11 60 views
0

我需要从CakePHP对Hudson CI服务器进行HTTP POST调用。该调用是参数化的并包含一些键/值对。其中一些实际上是需要上传的文件。在CakePHP中使用http_socket通过POST请求发送文件?

由于我使用CakePHP,我宁愿使用其自带的框架,而不是尝试写我自己的卷曲根据实施HttpSocket类。

到目前为止,调用如下:

$result = $http->post($apiCall, $params, $auth);

$ apiCall是哈德森REST API的URI。 $ params是与POST调用一起使用的参数数组。 $ auth包含一个用户/通过基本身份验证配置与这是哈德逊的实例。

我有点疑惑,虽然什么,我需要做的也包括在我的$参数数组文件?

我们正在运行哈德森v1.371它应该 - 据我已经收集 - 支持文件上传从参数化构建的呼叫来了。

谢谢!

+0

我在同一条船上。我不认为HTTPSocket类可以在本地执行此操作。最好的解决方案似乎是手动创建多部分内容数据。 – pixelastic 2011-07-22 15:23:11

回答

1

我不知道,如果HttpSocket类处理多HTTP请求。但是你可以手动创建它。我已在CakePHP GData Plugin中完成此操作,其中包含将视频上传到YouTube的功能。在YouTubeVideo model的保存方法创建包含有关视频和第二部分的元数据的XML文档的第一部分多Http请求正在上传视频文件的二进制内容:

// The boundary string is used to identify the different parts of a 
// multipart http request 
$boundaryString = 'Next_Part_' . String::uuid(); 

// Build the multipart body of the http request 
$body = "--$boundaryString\r\n"; 
$body.= "Content-Type: application/atom+xml; charset=UTF-8\r\n"; 
$body.= "\r\n"; 
$body.= $doc->saveXML()."\r\n"; 
$body.= "--$boundaryString\r\n"; 
$body.= "Content-Type: {$data[$this->alias]['file']['type']}\r\n"; 
$body.= "Content-Transfer-Encoding: binary\r\n"; 
$body.= "\r\n"; 
$body.= file_get_contents($data[$this->alias]['file']['tmp_name'])."\r\n"; 
$body.= "--$boundaryString--\r\n"; 

$this->request = array(
    'method' => 'POST', 
    'uri' => array(
    'host' => 'uploads.gdata.youtube.com', 
    'path' => '/feeds/api/users/default/uploads', 
), 
    'header' => array(
    'Content-Type' => 'multipart/related; boundary="' . $boundaryString . '"', 
    'Slug' => $data[$this->alias]['file']['name'] 
), 
    'auth' => array(
    'method' => 'OAuth', 
), 
    'body' => $body, 
); 

这可能会你在那里。

1

我试着用典型语法$result = $HttpSocket->post($url,$data,$request);,它为我工作。顺便说一句,有一些情况下,httpsocket发送空白数据(我还没有解决),所以我用cURL来代替这个。

此外,请确保您使用多部分窗体为您的窗体。 :)