Page 1 of 1

form post to curl post

Posted: Thu Jan 13, 2011 9:50 am
by Anant
HI,

I have a situation where i need to send a csv file to external site via post method - The first method i come up with is using form as shown below -

Code: Select all

<form name="file" enctype="multipart/form-data" method="post" action="http://server.com/abc/upload_data.php">

<input name=<?php echo $id ?>value="" type="file" size="30" /> <br />
<input name="profileName" value="<?php  echo "testing"; ?>" />
<input type="submit" value="ok" />

this works absolutely fine but problem with this is - user has to browse the file and then click ok to finish the task whereas i want it fully automated - as soon as this page runs - the form is submitted itself. So to overcome this issue - i come with equivalent curl option as shown below -

Code: Select all


function datapost($URLServer,$postdata)
		{

				$agent = "Mozilla/5.0";			 
				$cURL_Session = curl_init();
				curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);
				curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);
				curl_setopt($cURL_Session, CURLOPT_POST, 1);
				curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);
				curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);
				$result = curl_exec ($cURL_Session);
				 
				return $result;
				 
				}
				
				
				$postdata = array();
				$postdata['profileName'] = 'testing';
				$postdata[$id] = ' ';
				$postdata['file'] = '@'.'test/file/export/beta.csv';
				$postdata['submit'] = urlencode('submit');
				
			
			$abc= datapost("http://server.com/abc/upload_data.php",$postdata);
			echo $abc;

but this is not really working as i am getting blank page and it's not uploading the file to the external site.

Am i doing this curl bit correctly ? I am using the same method for initial request(to post few values and not file) and it works fine but just not working in this situation...

Any help would be greatly appreciated.

Thanks

Re: form post to curl post

Posted: Thu Jan 13, 2011 10:38 am
by Neilos
Anant wrote:

Code: Select all

$abc= datapost2("http://server.com/abc/upload_data.php",$postdata);
echo $abc;
Should it be;

Code: Select all

$abc= datapost("http://server.com/abc/upload_data.php",$postdata);
echo $abc;

Re: form post to curl post

Posted: Thu Jan 13, 2011 10:50 am
by Anant
sorry that' how it is - i made mistake while copying it here...

Re: form post to curl post

Posted: Thu Jan 13, 2011 7:54 pm
by Riquez
The curl method seems fine.
But the problem is deeper, it cant ever possibly work with curl

$postdata['file'] = '@'.'test/file/export/beta.csv';

Where is this CSV file exactly? If it is on the users HD how can PHP running on the server access it?
It cant unless it could somehow tunnel though the internet, log in to the computer & read the file.

When the user uses the form to click "browse" & selects the csv file, that is a client side action using the local browser to read a local file & put the contents of the csv file into the form "file" element. You cant perform that action with PHP running on a server somewhere else.

What you would need to do is have a client side method to read the file in, like with javascript or something.
But then whats the point of using curl.

I think the best way to do this would be to have your form, but make the file input hidden & make the file input value hard coded to the location of the csv file.
Then just have the button to say "upload csv file" & it posts normally to the url, no need for curl.

<input name="csvfile" value="test/file/export/beta.csv" type="file" size="30" />

It might take a bit of trial and error because i'm not sure this will work either. I'm not sure what the value of the input should be, is it a full local path?
I suspect its going to be different depending on the browser & the operating system etc, so its not a good method really.

Probably better off just having the form with browse button & submit as you started with.

Re: form post to curl post

Posted: Fri Jan 14, 2011 3:54 am
by Anant
HI,

Sorry if i wasn't too clear in my question.

Basically exactly what happening is -

1.) We posting initial data to external company - They check the post data and if it's ok they send us back an ID = This bit is working.
2.) Now once we receive the id - we need to send the data (which is a csv file) to external company via post. Once they receive the data they do what they are suppose to do with the data.

So it's all fine but we want this process to be fully automated which it is except the last uploading CSV file bit -

CSV file is in our remote server (Basically, we quering our database and exporting the result to csv file..)

So there shouldn't be any problem in accessing the file from remort server i guess ?

I hope this make more sense now..

Thanks

Re: form post to curl post

Posted: Fri Jan 14, 2011 6:10 am
by Riquez
Ahh OK, so in that case you should be able to read in the csv data like this.

Code: Select all

$postdata['file'] = file_get_contents('test/file/export/beta.csv');

Re: form post to curl post

Posted: Fri Jan 14, 2011 10:46 am
by Anant
Thanks this works but still got an error -

Error : uploaded file is either missing, empty or was not uploaded via the POST method

It's not missing and is not empty for sure as i have echoed it to web page. not too sure about the third one ?
Is it uploaded via the POST method ??
as i have included - curl_setopt($cURL_Session, CURLOPT_POST, 1);

Re: form post to curl post

Posted: Mon Jan 17, 2011 5:55 am
by Anant
Any clue guys ??