2013-06-27 58 views
6

我必须通过php中的cURL发送数据二进制参数。
这是命令:curl -D - -u user:password -X PUT -H "Content-Type: text/plain" --data-binary "data-id=2010-10-01_15-15-53" https://someurl。在控制台这个工程,现在我必须在PHP中做到这一点。cURL中的数据二进制参数

这是我的代码:

$this->_curl = curl_init(); 
    curl_setopt($this->_curl, CURLOPT_USERPWD, $this->_loginUser . ":" . $this->_loginPassword); 
    curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($this->_curl, CURLOPT_HEADER, 1); 
    curl_setopt($this->_curl, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($this->_curl, CURLOPT_TIMEOUT, 30); 
    curl_setopt($this->_curl, CURLOPT_URL, $this->_serviceUrl);//https://someurl 
    curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 
    curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, 'PUT'); 
    curl_setopt($this->_curl, CURLOPT_POSTFIELDS, array('data-id' => $dataId));//d'2010-10-01_15-15-53' 

    $response = curl_exec($this->_curl); 
    //$response = HTTP/1.1 201 Created 
    curl_close($this->_curl); 

呼叫服务器接受,但它不承认数据-ID参数:

无数据-ID财产定义触发2010-10-01_15-15-53

任何想法我失踪了?

+0

你摸不着头脑? – NCoder

+0

@NCoder nope :( – OSdave

+0

我假设这是为了surescripts集成。你有他们的基本连接设置发送和接收与CURL/PHP的基本请求? – NCoder

回答

4

您需要将您的字符串转换为先流。

你可以简单地用这段代码来做。

$YourString = 'data-id=2010-10-01_15-15-53'; 
$stream = fopen('php://memory','r+'); 
fwrite($stream, $YourString); 
$dataLength = ftell($stream); 
rewind($stream); 

然后有了你的流,你可以用curl发送它。

$curl = curl_init(); 
curl_setopt_array($curl, 
     array(CURLOPT_CUSTOMREQUEST => 'PUT' 
     , CURLOPT_URL => 'https://someurl' 
     , CURLOPT_HTTPHEADER => array(
      'Content-Type: text/plain' 
     ) 
     , CURLOPT_RETURNTRANSFER => 1      // means output will be a return value from curl_exec() instead of simply echoed 
     , CURLOPT_TIMEOUT => 15       // max seconds to wait 
     , CURLOPT_FOLLOWLOCATION => 0      // don't follow any Location headers, use only the CURLOPT_URL, this is for security 
     , CURLOPT_FAILONERROR => 0      // do not fail verbosely fi the http_code is an error, this is for security 
     , CURLOPT_SSL_VERIFYPEER => 1      // do verify the SSL of CURLOPT_URL, this is for security 
     , CURLOPT_VERBOSE => 0       // don't output verbosely to stderr, this is for security 
     , CURLOPT_INFILE => $stream 
     , CURLOPT_INFILESIZE => $dataLength 
     , CURLOPT_UPLOAD => 1 
     )); 

$response = curl_exec($curl); 
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
curl_close($curl); 
echo($response.'<br/>'); 
echo($http_code.'<br/>'); 

这应该工作。 ,帮助您的线条突出如下:

CURLOPT_INFILE => $流

CURLOPT_INFILESIZE => $ DATALENGTH

CURLOPT_UPLOAD => 1

0

为什么你想要传递纯文本为二进制文本?

--data-binary "data-id=2010-10-01_15-15-53" 

在这一行,你说你要传递纯文本,而不是二进制:

curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 

在我想你的主要问题是使用PUT方法任何情况下。 (https://stackoverflow.com/a/8054241/333061

我建议你使用POST方法是这样的:

curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array(
    'Accept: */*', 
    'Connection: Keep-Alive', 
    // this allows you transfer binary data through POST 
    'Content-type: multipart/form-data' 
    )); 
curl_setopt($this->_curl, CURLOPT_POST, 1); 
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 
    http_build_query(array('data-id' => $dataId))); 

要轻松通过卷曲/ PHP的传输二进制数据,看看这个:https://stackoverflow.com/a/3086357/333061

+0

作为二进制的文本和PUT方法是我正在沟通的服务以及必要条件:如果你看看我正在尝试php-ize的命令,你会发现它将内容设置为纯文本,并将数据标识作为二进制传递。我刚刚尝试使用POST的建议,但随后调用不被服务器接受。thanx反正 – OSdave