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

时间:2015-01-08 20:07:37

标签: php curl

我正在尝试转换以下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

以下是看似没问题的代码,但这不起作用:

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

似乎该文件实际上是上传了PHP代码,但在任何地方我都看到curl_file_create()的用法似乎很好。

1 个答案:

答案 0 :(得分:0)

尝试this previous answer,将graph-uri替换为file_contents。此外,您正在设置" graph-uri"作为可能发生冲突的GET和POST参数,图形-UR GET参数不是URL编码的,它在shell与PHP中的行为可能不同。所以你可以尝试以下方法:

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