Solved

Step by step php curl file handling

Posted on 2011-03-14
14
597 Views
Last Modified: 2012-05-11
I have a pdf form that submits to a form handler and writes out a .fdf file

I need to flatten the two (.pdf, .fdf) but hosting provider doesnt allow any tools :(
 I would like to pass the two files (in a secure way) to another server to flatten using pdftk and return the flattened .pdf file to be stored on the original calling server.

server 1 hosts my.pdf
my.pdf submits to server1 formhandler.php
server 1 outputs .fdf file and currently sends to a thanks page
ALL WORKS SO FAR

instead of the thanks page i want server1 to  open connection(curl) on server2 pass in the fdf and the pdf have server2 flatten them and return completed pdf

server1 then saves pdf for later viewing

I know i kind of double stated the problem but i wanted to show work flow.
The mode must be secure due to the data, but i have full control of server 2 and can adjust to fit needs of needed can use any protocol to get this working. URGENT

THANKS IN ADVANCE :)
Mark
0
Comment
Question by:mcornell1320
  • 8
  • 5
14 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35137451
What is the question?  Is there something we can answer for you?
0
 

Author Comment

by:mcornell1320
ID: 35138460
Looking for step by step example
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 35139283
You have to enter some variables for the connection, for example see below, where the $url is the url you want to connect to and the POSTFIELDS is the array of values you want to send, and they are all split by the connector. If you are using Godaddy as a host, there are more values you need, but for most hosts, this is all you need.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim(array_of_values, "connector"));

Open in new window

0
 

Author Comment

by:mcornell1320
ID: 35139479
Server1 is using godaddy server2 is not
0
 
LVL 17

Accepted Solution

by:
nanharbison earned 500 total points
ID: 35142020
You will need the Godaddy setop values, here is an example:
http://davidwalsh.name/godaddy-hosting-curl
0
 

Author Comment

by:mcornell1320
ID: 35143712
I still need example on passing to files and  the receiving handler
0
 

Author Comment

by:mcornell1320
ID: 35144374
SERVER 2 is getting the files and flattening the pdf correctly. the problem now is how do i pass the flattened pdf back to SERVER1

alot of the code to get this working came from the link provided for reference.

I just need to return the file now and then we can close and award points.

THANKS

// SUBMITTING SCRIPT SERVER1 
$target_url="http://server2/form5.php";
$file_name_with_full_path = "emptyform.pdf";
$file_name2 = "results/datafile.fdf";
$file="testme.pdf";
$file_to_upload = array(
    "file_contents"=>"@emptyform.pdf" ,
    "file_contents2"=>"@results/datafile.fdf",

); 


$fh = fopen('test.pdf', 'x');  
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE,1); 
curl_setopt($ch, CURLOPT_URL,$target_url);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
//curl_setopt($ch, CURLOPT_FILE, $fh);  
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload); 

$result=curl_exec ($ch); 



//cURL code is exactly the same as before...  

//Save file  


fwrite($fh, $result);  

fclose($fh); 

curl_close ($ch); 

echo $result;



//RECEIVING SCRIPT ON SERVER2
//Check for valid upload
if($_FILES['file_contents']['error'] != UPLOAD_ERR_OK) {
        echo 'Upload file error';
        return;
}
if($_FILES['file_contents2']['error'] != UPLOAD_ERR_OK) {
        echo 'Upload file error';
        return;
}
//Check for valid upload
if(!is_uploaded_file($_FILES['file_contents']['tmp_name'])) {
        echo 'Invalid request';
        return;
}
if(!is_uploaded_file($_FILES['file_contents2']['tmp_name'])) {
        echo 'Invalid request';
        return;
}
//Sanitize the filename (See note below)
$remove_these = array(' ','`','"','\'','\\','/');
$newname = str_replace($remove_these,'',$_FILES['file_contents']['name']);
$newname2 = str_replace($remove_these,'',$_FILES['file_contents2']['name']);
//Make the filename unique
$newname = time().'-'.$newname;
$newname2 = time().'-'.$newname2;
//Save the uploaded the file to another location
$upload_path = "/var/www/test/results/$newname";
$upload_path2 = "/var/www/test/results/$newname2";
move_uploaded_file($_FILES['file_contents']['tmp_name'], $upload_path);
move_uploaded_file($_FILES['file_contents2']['tmp_name'], $upload_path2);

 $test = passthru("pdftk $upload_path fill_form $upload_path2 output /var/www/test/results/new.pdf flatten");
echo $test;

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

by:nanharbison
ID: 35150470
Can't you just link to the file where it is? This is not an area of big expertise for me, perhaps it is an httprequest?
0
 

Author Comment

by:mcornell1320
ID: 35150776
No I can't store the data for privacy reasons
All data has to be removed after processing. I can do that but returning the file is holding me back
0
 
LVL 17

Assisted Solution

by:nanharbison
nanharbison earned 500 total points
ID: 35150826
Did you look into httprequest?
0
 

Author Comment

by:mcornell1320
ID: 35151126
I will tonight limited by mobile for now
0
 

Author Comment

by:mcornell1320
ID: 35178017

This is a working example for ssl on godaddy
other hosts if you set CURLOPT_SSL_VERIFYPEER to false it ignores CURLOPT_SSL_VERIFYHOST. On godaddy you you have to set both to false in order for it to work.

This is basic example with no error handling.

Points will goto nanharbison although he did not answer the question his link to davidwalsh website led me in the right direction.

THANKS
$file_name2="/path/to/file.fdf";
$file_to_upload = array(
    "file_contents"=>"@newclientintake.pdf" ,
    "file_contents2"=>"@" .$file_name2,

     );
$target_url="https://www.googletest.com/myscript.php";
//$fh="the file you want to store data in"

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload); 

// Call curl script to flatten
$result=curl_exec ($ch); 

//Save file  
fwrite($fh, $result);  

// Close Files
fclose($fh); 


// Close curl Object
curl_close ($ch);

Open in new window

0
 

Author Closing Comment

by:mcornell1320
ID: 35178027
I have accepted my own solution as after 9 hrs of searching and putting it together this is what works
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 35181867
Thanks for the points anyway. Was the fwrite how you got the document back? So the document was available the whole time?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Imagine a situation that you have installed SSL (http://en.wikipedia.org/wiki/Secure_Sockets_Layer) Certificate on your Cisco ASA (Cisco Adaptive Security Appliance) firewall. Installation of SSL certificate on ASA is an another topic for which you …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now