用PHP执行curl [英] Executing Curl with PHP

查看:136
本文介绍了用PHP执行curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Docverter 将LaTeX / markdown文件转换为PDF,但使用PHP时遇到问题可通过通过其API访问Docverter 来执行CURL。我知道我不是一个完全白痴b / ci可以得到这个工作适应shell脚本

使用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);

这不会提供错误消息,但不起作用。

更新基于@ John的建议,下面是一个使用PHP curl_exec()的例子: 改编自这里。不幸的是,这也不工作,但至少它提供错误消息。

  $ url ='http://c.docverter .com / convert'; 
$ fields_string ='';
$ fields = array('from'=>'markdown',
'至'=>'pdf',
'input_files []'=> $ _SERVER ['DOCUMENT_ROOT ']。'/ markdown.md',
);

// url-ify POST
的数据foreach($ fields为$ key => $ value){$ fields_string。= $ key。'='。$ value。 '&'; }
rtrim($ fields_string,'&');

//打开连接
$ ch = curl_init();

//设置url,POST变量的数量,POST数据
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,count($ fields));
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ fields_string);

//执行post
$ result = curl_exec($ ch);

//关闭连接
curl_close($ ch);


解决方案

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



1) $ fields 数组格式不正确, code> input_files [] 。它需要一个 @ / 和mime类型声明(见下面的代码)



2) curl_exec()输出(实际新创建的文件内容),而不仅仅是 true / false 。这是通过设置curl选项 curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); (见下面的代码)来实现。



完整工作示例

  //设置POST变量
$ url ='http ://c.docverter.com/convert';
$ fields = array('from'=>'markdown',
'至'=>'pdf',
'input_files []'=>@ /。 realpath('markdown.md')。;; type = text / x-markdown; charset = UTF-8
);

//打开连接
$ ch = curl_init();

//设置选项
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); //需要,以便$ result = curl_exec()输出是文件,不只是true / false

//执行post
$ result = curl_exec($ ch);

//关闭连接
curl_close($ ch);

//写入文件
$ fp = fopen('uploads / result.pdf','w'); //确保目录markdown.md在,并且result.pdf将转到具有适当的权限
fwrite($ fp,$ result);
fclose($ fp);


I'm trying to use Docverter to 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 example and running from command line (Mac OSX).

Using PHP's 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?

UPDATE Based 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.

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

解决方案

I solved my own problem. There were two main problems with the above code:

1) The $fields array was incorrectly formatted for the input_files[]. It needed a @/ and mime-type declaration (see code below)

2) The curl_exec() output (the actual newly created file contents) needed to be returned and not just true/false which is this function's default behavior. This is accomplished by setting the curl option curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); (see code below).

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

这篇关于用PHP执行curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆