数据二进制参数在cURL [英] data-binary parameter in cURL

查看:207
本文介绍了数据二进制参数在cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过cURL在php中发送数据二进制参数。

这是命令: curl -D - -u user:password -X PUT -HContent -Type:text / plain--data-binarydata-id = 2010-10-01_15-15-53https:// someurl 。在控制台这个工作,现在我必须在php。



这是我有的代码:

  $ 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,类型: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创建
curl_close($ this-> _curl);

服务器接受调用,但不能识别 data-id 参数:


触发器中未定义数据ID属性2010-10-01_15-15-53


任何想法我错过了什么?

解决方案



您可以直接使用这段代码。

  $ 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 //表示输出将是curl_exec()的返回值,而不是简单地回显
,CURLOPT_TIMEOUT => 15 //最大秒等待
,CURLOPT_FOLLOWLOCATION => 0 //不跟随任何Location头,只使用CURLOPT_URL,这是为了安全
,CURLOPT_FAILONERROR => / do not fail verbosely fi http_code是一个错误,这是为了安全
,CURLOPT_SSL_VERIFYPEER => 1 //验证CURLOPT_URL的SSL,这是为了安全
,CURLOPT_VERBOSE => 0 / /不输出verbosely到stderr,这是为了安全
,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 => $ stream



CURLOPT_INFILESIZE => $ dataLength



CURLOPT_UPLOAD => 1 b

I have to send data-binary parameter through cURL in php.
This is the command: curl -D - -u user:password -X PUT -H "Content-Type: text/plain" --data-binary "data-id=2010-10-01_15-15-53" https://someurl. In the console this works, now I have to do it in php.

This is the code I have:

    $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);

the call is accepted by the server, but it doesn't recognize the data-id parameter:

No data-id property defined in trigger 2010-10-01_15-15-53

Any idea what I'm missing?

解决方案

You need to convert your string to stream first.

You can simply do it with this piece of code.

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

Then having your stream, you can send it using 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/>');

This should work. The lines that help you are highlighted below:

CURLOPT_INFILE => $stream

CURLOPT_INFILESIZE => $dataLength

CURLOPT_UPLOAD => 1

这篇关于数据二进制参数在cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆