使用PUT方法与PHP cUrl库 [英] Using PUT method with PHP cUrl Library
本文介绍了使用PUT方法与PHP cUrl库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以成功运行以下curl命令(在命令行):
I'm able to run the following curl command (at the command line) successfully:
curl -XPOST --basic -u user:password -H accept:application/json -H Content-type:application/json --data-binary '{ "@queryid" : 1234 }' http://localhost/rest/run?10
这里是我到目前为止做的,但它似乎并不使用REST服务'm using:
Here is what I'm doing so far however it doesn't seem to be working with the REST service I'm using:
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$url = 'http://localhost/rest/run?10';
$query = '{ "@queryid" : 1234 }';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_POSTFIELDSIZE, strlen($query));
$output = curl_exec($ch);
echo $output;
尝试使用PUT方法转换--data-binary时,正确的方法是什么? p>
What is the correct way when trying to convert --data-binary using a PUT method?
推荐答案
而不是在磁盘上创建一个临时文件,你可以使用 php:// temp
。
Instead of creating a temp file on disk you can use php://temp
.
$body = 'the RAW data string I want to send';
/** use a max of 256KB of RAM before going to disk */
$fp = fopen('php://temp/maxmemory:256000', 'w');
if (!$fp)
{
die('could not open temp memory data');
}
fwrite($fp, $body);
fseek($fp, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
上面是没有磁盘IO,所以它应该更快,
The upside is no disk IO so it should be faster and less load on your server.
这篇关于使用PUT方法与PHP cUrl库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文