用PHP执行Curl

用PHP执行Curl,php,curl,latex,markdown,docverter,Php,Curl,Latex,Markdown,Docverter,我正在尝试使用将LaTeX/markdown文件转换为PDF,但在使用PHP执行CURL时遇到问题。我知道我不是一个十足的白痴b/c我可以通过修改shell脚本并从命令行(MacOSX)运行来实现这一点 使用PHP的exec(): 这不会给出错误消息,但不起作用。在什么地方有路径问题吗 UPDATE根据@John的建议,下面是一个使用PHP的curl\u exec()改编自的示例。不幸的是,这也不起作用,尽管它至少给出了错误消息 $url = 'http://c.docverter.com/co

我正在尝试使用将LaTeX/markdown文件转换为PDF,但在使用PHP执行CURL时遇到问题。我知道我不是一个十足的白痴b/c我可以通过修改shell脚本并从命令行(MacOSX)运行来实现这一点

使用PHP的
exec()

这不会给出错误消息,但不起作用。在什么地方有路径问题吗

UPDATE根据@John的建议,下面是一个使用PHP的
curl\u 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);

我解决了自己的问题。上述代码存在两个主要问题:

1)
输入文件[]
$fields
数组格式不正确。它需要一个
@/
和mime类型声明(参见下面的代码)

2) 需要返回
curl\u exec()
输出(实际新创建的文件内容),而不仅仅是
true/false
,这是此函数的默认行为。这是通过设置curl选项
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true)实现的(参见下面的代码)

完整的工作示例

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

你有没有试过
shell\u exec
?——@Dainis,还没有,我已经让
exec()
做了其他事情,并且承认我不确定与
shell\u exec()的区别。我不想作为shell脚本运行的原因是,文件名和路径将发生变化,所以我需要这些变量。为什么不使用为PHP编写的curl函数而不是exec?@John,我很乐意使用PHP的
curl()
我也不确定如何做到这一点。我让
exec()
去做其他的事情,所以这就是我开始做的。您能否使用
curl()
提供答案?请查看
curl\u init
curl\u setopt
curl\u exec
的文档。
//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);