jmwebguy Posted June 13, 2007 Share Posted June 13, 2007 I need some help with some sample code I found. I have a Windows server with PHP 5 installed. Other code is working. I am trying to post an XML file to a URL. Current code: <? php $curl = curl_init(); $fp = fopen("client.xml", "w"); curl_setopt ($curl, CURLOPT_URL, "https://www.xxxxxx.com/filename.php"); curl_setopt($curl, CURLOPT_FILE, $fp); curl_setopt($curl, CURLOPT_POSTFIELDS, $fp); // add POST fields curl_exec ($curl); curl_close ($curl); ?> XML file looks like: <?xml version="1.0"?> <SSTransaction> <SystemName>myname</SystemName> <IPAddress>1.1.1.1</IPAddress> <SSAuth> <Admin>admin</Admin> <Pass>pass</Pass> </SSAuth> <ClientTrans> <ClientAdd> <Client> <Username>client13</Username> <Password>clientpass13</Password> <Orgname>organization13</Orgname> <FirstName>John</FirstName> <LastName>Smith</LastName> <EmailAddress>client13@hotmail.com</EmailAddress> <BusPhone>123-bus</BusPhone> <HomePhone>123-home</HomePhone> <Fax>123-fax</Fax> <MobPhone>123-mobile</MobPhone> <Address1>st1</Address1> <Address2>st2</Address2> <City>city</City> <Province>AL</Province> <PostalCode>zip</PostalCode> <Country>Canada</Country> <sAddress1>secs1</sAddress1> <sAddress2>ses2</sAddress2> <sCity>seccity</sCity> <sProvince>AK</sProvince> <sPostalCode>seczip</sPostalCode> <sCountry>Canada</sCountry> </Client> </ClientAdd> </ClientTrans> </SSTransaction> What is wrong with my CURL code. It's the only code on my php page. Quote Link to comment https://forums.phpfreaks.com/topic/55423-newbie-needs-curl-help/ Share on other sites More sharing options...
mr_zhang Posted June 13, 2007 Share Posted June 13, 2007 I think you are forgetting this line: curl_setopt($ch, CURLOPT_POST, 1); Without it, curl won't post. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/55423-newbie-needs-curl-help/#findComment-273906 Share on other sites More sharing options...
jmwebguy Posted June 13, 2007 Author Share Posted June 13, 2007 Ok this is still not working. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/55423-newbie-needs-curl-help/#findComment-273934 Share on other sites More sharing options...
mr_zhang Posted June 13, 2007 Share Posted June 13, 2007 try add: curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); then change the curl_exec ($curl); into $tmp=curl_exec ($curl); echo $tmp; It will then include the http headers. Read the response code from the server. If it's not code 200 then it's not the CURL, perhaps it's the (their) server setting or the xml formatting. If it's code 200 but not submitting, then you are missing some variables or you are posting to a non-posting page. View the source code to get the real page and the complete variables. Quote Link to comment https://forums.phpfreaks.com/topic/55423-newbie-needs-curl-help/#findComment-273950 Share on other sites More sharing options...
jmwebguy Posted June 13, 2007 Author Share Posted June 13, 2007 Here is what I have now: <?php $myFile = "http://www.myurl.com/client.xml"; $fh = fopen($myFile, 'r'); $theData = fread($fh, 50000); fclose($fh); echo $theData; ?> <? php $Url="https://www.anotherdomain.com/xmlCapture.php"; $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, $Url); curl_setopt($curl, CURLOPT_FILE, $theData); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $theData); $tmp=curl_exec ($curl); echo $tmp; curl_close ($curl); ?> Anything wrong with this code? Nothing is displaying. Quote Link to comment https://forums.phpfreaks.com/topic/55423-newbie-needs-curl-help/#findComment-274013 Share on other sites More sharing options...
mr_zhang Posted June 14, 2007 Share Posted June 14, 2007 I think the problem is because you use CURLOPT_FILE and CURLOPT_POSTFIELDS together. Try erasing the CURLOPT_FILE. Don't forget that the CURLOPT_POSTFIELDS is a string with format: 'var1=value1&var2=value2&var3=value3' If it's still not working, try to get the CURL error then. ... $tmp=curl_exec ($curl); curl_close ($curl); if ($tmp) {echo $tmp;} else {echo curl_error($curl);} Quote Link to comment https://forums.phpfreaks.com/topic/55423-newbie-needs-curl-help/#findComment-274332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.