[英]Unix command line CURL equivalent in PHP CURL
以下代码片段来自“ http://pdfx.cs.man.ac.uk/usage ”。 这个非常棒的工具,它将pdf格式的科学论文转换为xml。
curl --data-binary @"/path/to/my.pdf"
-H "Content-Type: application/pdf"
-L "http://pdfx.cs.man.ac.uk"
这段代码是unix命令行代码,我想要它的PHP版本。 我努力了
$pdfFile = fopen('jucs_18_05_0623_0649_hasan.pdf', 'r');
$fileSize = filesize ('jucs_18_05_0623_0649_hasan.pdf');
$url="http://pdfx.cs.man.ac.uk";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $pdfFile);
curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$fp = fopen("test.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
if (! $res = curl_exec($ch))
echo "Error: ".curl_error($ch);
else {
echo "Success";
}
curl_close($ch);
问题是test.xml的输出是索引文件的html代码,而不是提供的文章的转换后的xml版本。
等待您的专家意见...
提前致谢
不需要。 需要内容长度。
<?php
$pdfFile = fopen('1.pdf', 'r');
$fileSize = filesize ('1.pdf');
$url="http://pdfx.cs.man.ac.uk";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/pdf","Content-length: ".$fileSize));
curl_setopt($ch, CURLOPT_INFILE, $pdfFile);
curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$fp = fopen("test.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
if (! $res = curl_exec($ch))
echo "Error: ".curl_error($ch);
else {
echo "Success";
}
curl_close($ch);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.