Akshay Wani
Akshay Wani

Reputation: 33

PHP cURL not working for sending POST request to api

I am trying to send post request to a SMS api and receive the feedback from them using PHP and cURL .But, the cURL is not working for me ,when i try to send the same data through form it works.

$POST = array(
            'data' => $xml1

    );
$url = 'URL';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $POST );
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo "<input type=\"textarea\" id=\"txt\"  style='width:600px' value='$result'></input>";
echo "<input type=\"textarea\" id=\"txt1\" name='data1' style='width:600px' value='".$info['request_header']."'></input>";

And the XML IS

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE MESSAGE SYSTEM "http://127.0.0.1:80/psms/dtd/messagev12.dtd">
<MESSAGE VER="1.2">
<USER USERNAME="abc" PASSWORD="pqr" />
<SMS UDH="0" CODING="1" TEXT="SMS TEXT" PROPERTY="0" ID="1" TEMPLATE="" EMAILTEXT="" ATTACHMENT=""><ADDRESS FROM="someone" TO="91xxxxxxxxxx" EMAIL="" SEQ="1" TAG="some clientside random data"/>
</SMS>
</MESSAGE>

BUT USING FORM IT WORKS FINE ,CODE FOR FORM

echo "<form action=\"URL\" method=\"POST\" target=\"_blank\">";

    echo "<input type=\"textarea\" id=\"txt\" name='data' style='width:600px' value='$xml1'></input>";
    echo "<input type=\"SUBMIT\" name=\"action\" value=\"send\">";
    echo "</form>";

When I send data using form it shows Sent status but when i send using curl it shows output as :

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <MESSAGEACK>
    <Err Code="65535" Desc="The Specified message does not conform to DTD"/>
</MESSAGEACK> 

Thanks

Upvotes: 1

Views: 973

Answers (2)

Manu Agarwal
Manu Agarwal

Reputation: 47

instead of creating array.

$POST = array('data' => $xml1);

Simply do $POST => $xml1

Upvotes: 1

Mahshad
Mahshad

Reputation: 13

How did you set $xml1 variable?

Make sure you used $xml1 = file_get_contents($xmlFilepath) istead of simplexml_load_file($xmlFilepath)

Upvotes: 0

Related Questions