Web Hosting Talk







View Full Version : PHP Curl Help


Omni
06-27-2007, 03:31 PM
Hi all, been at this for the past 6 hours and it's probably something simple for a php expert.

Problem
I am trying to use curl to emulate submitting an html form using POST method. So far I have been able to get it working for all forms. The only exception is one that also needs to upload a file. My understanding is that this is a multi part POST.

$params="key1=data1&key2=data2&key3=data3";
$tempfile = "filename.ext";

$ch=curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,array('csv_file'=>"@$tempfile"));
curl_setopt($ch, CURLOPT_POSTFIELDS,array($params));
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_COOKIE, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result=curl_exec ($ch);
curl_close ($ch);


echo $result;

This didn't work though. I've tried many iterations of formatting CURLOPT_POSTFIELDS to no avail.

Any ideas?

epcmedia
06-27-2007, 03:43 PM
Hi Omni,

In the PHP docs on php.net there is a mention of a bug with regard to file uploads and curl.

http://us2.php.net/manual/en/ref.curl.php#75193


Basically, the problem is that you must pass the fill path of the file you want to upload. The solution is to:

Solution - use absolute path for uploaded file. As:
curl_setopt($ch, CURLOPT_POSTFIELDS, array("userfile" => '@'.realpath('filename.ext'));

I hope that helps a little. Let me know if that works. Cheers!

EPCMEDIA

Omni
06-27-2007, 04:00 PM
epcmedia you rock :D works now