PDA

View Full Version : Curlopt_post



dophinsluv
03-29-2009, 08:35 AM
Hi Bluehostforum readers,

This code comes from the php manual entry for curl_setopt (http://www.php.net/manual/en/function.curl-setopt.php).

Does this work for you? Any ideas on how to get it working? Running this code just gives me a blank browser screen.



<?php

/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
?>

wysiwyg
03-29-2009, 09:22 AM
Unless you've changed something, there's a very good reason it's blank.

The file needs to be the correct path to an actual file (the @ in front is important), and the curlopt_url needs to be an actual url containing the two lines that are commented out at the top of the script.

dophinsluv
04-05-2009, 09:36 AM
Thanks for responding wysiwyg.

This code still doesn't work for me. Here are some questions I have.

Is there anything special about the POST operation? Can someone describe step by step the POST and how it differs from the GET?

Is there something about the bluehost.com environment that might be causing this not to work? For example, is this using traditional http ports that are available to all?

Why is the file important? I was taking this to be a pure data string, so I left it as is. If the file is accessed, what does post due with it? If this is a client side file, which I'm now suspecting, and I'm using Windows does the "full path" look like @C:\Documents and Settings\username\Desktop\image.png (which I tried and doesn't work)?

Also, I have been including upload.php as a separate file on the server. These are the first few commented lines in the code from the original post that wysiwyg referred to. So, I don't believe this is the issue.

wysiwyg
04-07-2009, 11:17 AM
The difference is basically that GET stores and retrieves variables from the URL (along with all the restrictions that implies), and POST doesn't.

To debug this script, change curl_exec.. to curl_exec($ch) or die( curl_error($ch) ).

Also, remove the @ from the file path to post it as a string, rather than a file. If it works like that, then your path is definitely wrong (your example @C:\Doc.. is fine).

The file is important because you're telling it to read the file into a post variable, and if that file doesn't exist it's going to fail catastrophically.

If you're still getting a blank page after telling it to output any errors, there's probably something wrong with upload.php. I'd personally make a little more robust of a test page myself. Just having it print the two variables will return nothing at all if both arrays are empty.

dophinsluv
04-26-2009, 10:47 AM
I appreciate your help wsyiwyg.

I took out the @ sign and it partially worked. The POST occurs.

The problem seems to be in the file path. Could it be that the directory delimiters are getting mangled?



Array ( [name] => Foo [file] => C:\\filename.png ) Array ( ) This worked



Why does the path get an extra "\"? What type of path delimitter is expected?

dophinsluv
05-08-2009, 08:10 AM
If I replace

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

with

$data = array('name' => 'Foo', 'file' => 'c:\test.png');

I get the output:

Array ( [name] => Foo [file] => c:\\filename.png ) Array ( ) This worked

If I use

$data = array('name' => 'Foo', 'file' => 'c:\\test.png');

I get the exact same output.

What do you think?

Thanks in advance