gpt4 book ai didi

php - cURL 中的数据二进制参数

转载 作者:可可西里 更新时间:2023-11-01 13:22:56 25 4
gpt4 key购买 nike

我必须在 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 中完成。

这是我的代码:

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

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

No data-id property defined in trigger 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 // 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 => $stream

CURLOPT_INFILESIZE => $dataLength

CURLOPT_UPLOAD => 1

关于php - cURL 中的数据二进制参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com