PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 28th, 2008, 06:40 AM
i_hope i_hope is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 21 i_hope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 4 m 53 sec
Reputation Power: 0
PHP cURL. Send a file

Hi,

I have been looking for information about this problem for the last two days and nothing has worked as I needed, that's why I am writing here now.

I am working on connecting interfaces using XML messages and I have been always using cURL for this purpose with no problem. I just kept sending my xml message as a string inside a POST variable and always got the response with no much deal.

Since last week I have been requested to join the interface of the company where I work to a new channel we will join to. After reading the documentation of this new interface, I thought everything would just work the same way but it is not like this.

This new channel is indeed expecting to receive a xml FILE (not just a POST variable) and I have tried to send it in as many ways as I could think about and never worked. In fact, the only way I have gotten a real response from this channel was while using a <form> and a <input type="file" name="xmlfile"> and sending this file using the submit function of this form.

The problem is that in my code there is no place for a form, while everything is running internally with php code, being launched each time an update is needed.

I have been told that one call in the command line like this would work:

Code:
curl -F "xmlfile=@/test_2_send.xml" "http://url_where_to_send.html" > response_we_got.state.zip


These are the different ways I have already tried:


PHP Code:
 $ch curl_init();  
  
$putData tmpfile();
  
fwrite($putData$xml_request);
  
fseek($putData0);
  
  
$result_file fopen('/response_we_got.state.zip'"w");

  
/* Set cURL options. */
  
curl_setopt($chCURLOPT_URL'http://url_where_to_send.html');
  
curl_setopt($chCURLOPT_PUTtrue);
  
curl_setopt($chCURLOPT_INFILE$putData);
  
curl_setopt($chCURLOPT_INFILESIZEstrlen($xml_request));
  
curl_setopt($chCURLOPT_FILE$result_file);
  
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

  
$result curl_exec($ch);  
  
fclose($putData);        
  
curl_close($ch); 


This one I think would work just if I would be able to change the name of the INFILE element for "xmlfile"
PHP Code:
 $postfields = array(
                    
"xmlfile"     => "@/test_2_send.xml"
                    
); 
                    
$pushUrl curl_init('http://url_where_to_send.html');
curl_setopt($pushUrlCURLOPT_VERBOSE1);
#curl_setopt($pushUrl, CURLOPT_FAILONERROR, 1);
curl_setopt($pushUrlCURLOPT_FOLLOWLOCATION1);
curl_setopt($pushUrlCURLOPT_RETURNTRANSFERtrue);
curl_setopt($pushUrlCURLOPT_POSTtrue);
curl_setopt($pushUrlCURLOPT_FILE$result_file);
curl_setopt($pushUrlCURLOPT_POSTFILEDS$postfields);
$output curl_exec($pushUrl);

curl_close($pushUrl); 



From both of the solutions I am getting as response an error saying:

Code:
xmlfile is null, empty or doesn't exist.


I would be really pleased if anyone would be able to help me.

Thanks a lot,
Álvaro

Reply With Quote
  #2  
Old May 28th, 2008, 06:03 PM
TeraTask TeraTask is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Location: Reno, NV
Posts: 88 TeraTask User rank is Sergeant (500 - 2000 Reputation Level)TeraTask User rank is Sergeant (500 - 2000 Reputation Level)TeraTask User rank is Sergeant (500 - 2000 Reputation Level)TeraTask User rank is Sergeant (500 - 2000 Reputation Level)TeraTask User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 23 m 7 sec
Reputation Power: 13
After reading w ww.p hp.net/ma nual/en/fu nction.cu rl-set opt.php , I was able to write this:

PHP Code:
<?php
$file_to_upload 
= array('file_contents'=>'@'.$file_name_with_full_path);
$ch curl_init();
curl_setopt($chCURLOPT_URL,$target_url);
curl_setopt($chCURLOPT_POST,1);
curl_setopt($chCURLOPT_POSTFIELDS$file_to_upload);
$result=curl_exec ($ch);
curl_close ($ch);
echo 
$result;
?>


which worked just like in example 2 on the PHP site.

NOTE: On the URL above, I had to add spaces due to restrictions. I hope the admins don't get upset for me linking to the PHP site -- not like I'm trying to promote my own site.

Reply With Quote
  #3  
Old May 29th, 2008, 08:42 AM
i_hope i_hope is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 21 i_hope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 4 m 53 sec
Reputation Power: 0
Thank you very much for your answer. Surprisingly it has worked

And I say that I am surprised because the only thing that differences your code, from mine is:

YOURS
Code:
$file_to_upload = array('xmlfile'=>'@'.$file_name_with_full_path); 


MINE
Code:
 $postfields = array("xmlfile"=> "@/full_path_2_send.xml");  


So I assume it is important to write the @ symbol outside the string of the file's path...

Thanks again.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP cURL. Send a file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap