The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Post with cURL
Discuss Post with cURL in the PHP Development forum on Dev Shed. Post with cURL PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 2nd, 2012, 12:44 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
|
|
|
[Resolved] Post with cURL
Hello,
I'm trying to post a file from the local server to a remote server. I'm using cURL to do so, but it's not working right.
PHP Code:
$filename = '@'.getcwd()."/test.txt";
$vars[0] = "file=".urlencode($filename);
$vars[1] = "newName=".urlencode("success.txt");
$postString = implode('&', $getVars);
$url = "www.myserver.com/postProcess.php";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($vars));
curl_setopt($ch,CURLOPT_POSTFIELDS, $poststring);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
From everything I've read, this ('@'.getcwd()."/test.txt") should be pulling the file and posting it to the external server.
a print_r($_POST) just gives me these results though...
Code:
Array ( [file] => @/var/www/html/test.txt [newName] => success.txt )
The actual file itself is not being sent, just the path
Thanks to anyone who can help me out.
Last edited by jiceman : November 5th, 2012 at 08:07 AM.
Reason: problem solved
|

November 2nd, 2012, 12:46 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Quote: From everything I've read, this ('@'.getcwd()."/test.txt") should be pulling the file and posting it to the external server. | Where did you read that? It's not true. Use file_get_contents to get a file's contents.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

November 2nd, 2012, 01:40 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
|
|
Thank you Dan.
I read that the "@" symbol was supposed to tell cURL that what followed was a reference to a file on the local system, and to send that file during the post.
I'm trying to send a pdf file. The pdf file is 2 pages, and now I'm creating a blank 2 page pdf file.
I'm trying 2 different methods, but I get the same results of just a blank 2 page pdf.
PHP Code:
$handle = fopen("files/test.pdf", 'a');
fwrite($handle, $_POST['file']);
fclose($handle);
file_put_contents("files/test2.pdf", $_POST['file']);
|

November 2nd, 2012, 01:50 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Oh man, I totally brain farted on @. Yes, that's correct.
This code should work:
$data['Filedata'] = "@".$file_path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
return $response;
POSTFIELDS needs to have a variable called 'Filedata'. you can't just stick it into the array.
And once it makes it to the posted script, it will be an actual file upload, not a regular POST field.
|

November 2nd, 2012, 03:24 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
|
|
Dan, You're awesome. You are great help.
One last thing...
Is it possible to send the file like that and other variables as well, particularly an array.
So for example, what I was sending before (being unable to send the file) was something like this...
PHP Code:
$vars['names'][] = "a";
$vars['names'][] = "b";
$vars['names'][] = "c";
$vars['names'][] = "d";
$vars['names'][] = "e";
$vars['file'] = "@/var/www/html/file.pdf";
I then turned that into a GET type string. (like this: "var=value&var2=value2&var3=value3")
On the target server, the $_POST variable would recreate the arrays as desired.
the only issue was that the file wouldn't get sent.
So now the file is being sent, but if my array is something like...
PHP Code:
$vars['test'] = "test value";
$vars['names'][] = "a";
$vars['names'][] = "b";
$vars['names'][] = "c";
$vars['names'][] = "d";
$vars['names'][] = "e";
$vars['Filedata'] = "@/var/www/html/file.pdf";
And just the $vars is being posted in the POSTFIELDS instead of the string form,
Then the file does get posted like I wanted, but now the $_POST['names'] just equals the string "Array".
$_POST['test'] does equal "test value" though.
Is it possible to send the file, and data arrays, so that $_POST will recreate the arrays like it did before?
|

November 2nd, 2012, 05:06 PM
|
 |
Lost in code
|
|
|
|
Yes, something like this should work:
PHP Code:
$vars['names[0]'] = "a";
$vars['names[1]'] = "b";
$vars['names[2]'] = "c";
$vars['names[3]'] = "d";
$vars['names[4]'] = "e";
$vars['file'] = "@/var/www/html/file.pdf";
|

November 5th, 2012, 08:06 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
|
|
|
That makes sense now that I see it. Thank you E-Oreo.
It works perfectly.
Thanks again ManiacDan.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|