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


Converting a Curl command into PHP Curl code (file transfer & custom 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

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

$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()的任何地方,它的用法似乎都很好。

试试前面的答案,用 graph-uri 代替 file_contents。 此外,您将"graph-uri"设置为GET和POST参数,这可能会发生冲突,并且graph-uri GET参数不是URL编码的,这在shell中的行为可能与.PHP不同。 因此,您可以尝试以下操作:

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