用 PHP 执行 Curl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14899037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing Curl with PHP
提问by tim peterson
I'm trying to use Docverterto convert LaTeX/markdown files to PDF but am having trouble using PHP to do CURL to access Docverter via their API. I'm know I'm not a total idiot b/c i can get this to work adapting the shell script in this Docverter exampleand running from command line (Mac OSX).
我正在尝试使用Docverter将 LaTeX/markdown 文件转换为 PDF,但在使用 PHP 执行 CURL以通过其 API 访问 Docverter时遇到问题。我知道我不是一个彻头彻尾的白痴 b/ci 可以让它在这个 Docverter 示例中适应 shell 脚本并从命令行(Mac OSX)运行。
Using PHP's exec():
使用 PHP 的exec():
$url=$_SERVER["DOCUMENT_ROOT"];
$file='/markdown.md';
$output= $url.'/markdown_to_pdf.pdf';
$command="curl --form from=markdown \
--form to=pdf \
--form input_files[]=@".$url.$file." \
http://c.docverter.com/convert > ".$output;
exec("$command");
This gives no error messages but doesn't work. Is there a path issue somewhere?
这不会给出错误消息,但不起作用。某处是否存在路径问题?
UPDATEBased on @John's suggestion, here's an example using PHP's curl_exec()adapted from here. Unfortunately this also doesn't work though at least it gives error messages.
UPDATE基于对@约翰的建议,这里是使用PHP的一个例子curl_exec()改编自这里。不幸的是,这也不起作用,但至少它会给出错误消息。
$url = 'http://c.docverter.com/convert';
$fields_string ='';
$fields = array('from' => 'markdown',
'to' => 'pdf',
'input_files[]' => $_SERVER['DOCUMENT_ROOT'].'/markdown.md',
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
回答by tim peterson
I solved my own problem. There were two main problems with the above code:
我解决了我自己的问题。上面的代码有两个主要问题:
1) The $fieldsarray was incorrectly formatted for the input_files[]. It needed a @/and mime-type declaration (see code below)
1)$fields数组的格式不正确input_files[]。它需要一个@/和 MIME 类型的声明(见下面的代码)
2) The curl_exec()output (the actual newly created file contents) needed to be returned and not just true/falsewhich is this function's default behavior. This is accomplished by setting the curl option curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);(see code below).
2)curl_exec()需要返回输出(实际新创建的文件内容),而不仅仅是true/false此函数的默认行为。这是通过设置 curl 选项来实现的curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);(参见下面的代码)。
Full working example
完整的工作示例
//set POST variables
$url = 'http://c.docverter.com/convert';
$fields = array('from' => 'markdown',
'to' => 'pdf',
'input_files[]' => "@/".realpath('markdown.md').";type=text/x-markdown; charset=UTF-8"
);
//open connection
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //needed so that the $result=curl_exec() output is the file and isn't just true/false
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//write to file
$fp = fopen('uploads/result.pdf', 'w'); //make sure the directory markdown.md is in and the result.pdf will go to has proper permissions
fwrite($fp, $result);
fclose($fp);

