Jump to content

Newbie needs curl help


jmwebguy

Recommended Posts

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>[email protected]</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.

Link to comment
https://forums.phpfreaks.com/topic/55423-newbie-needs-curl-help/
Share on other sites

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.

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.

 

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);}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.