简体   繁体   English

将Curl命令转换为PHP Curl代码(文件传输和自定义命令)

[英]Converting a Curl command into PHP Curl code (file transfer & custom command)

I am trying to convert the following Curl command: 我正在尝试转换以下Curl命令:

curl --digest --user "username:password" --verbose --url "http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/" -X POST -T /data/datasets/foo.n3

Here is the code that appears to be ok, but that doesn't work: 这是看起来不错的代码,但是不起作用:

$args = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$args['graph-uri'] = curl_file_create('/data/datasets/foo.n3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);

It seems that the file is actually not uploaded with the PHP code, but everywhere I look the usage of curl_file_create() seems good. 似乎该文件实际上并未随PHP代码一起上传,但在我看来, curl_file_create()的用法似乎都不错。

Try this previous answer , substituting graph-uri for file_contents. 试试这个先前的答案 ,用graph-uri代替file_contents。 Also, you are setting "graph-uri" as both GET and POST params, which could collide, and the graph-uri GET param is not URL encoded, which could behave differently in the shell vs. PHP. 另外,您将“ graph-uri”设置为GET和POST参数,这可能会冲突,并且graph-uri GET参数不是URL编码的,在shell和PHP中的行为可能不同。 So you might try the following: 因此,您可以尝试以下操作:

http://127.0.0.1/ws?graph-uri=http%3A%2F%2Flocalhost%2Fdataset%2Fimport%2F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM