4

I have a XML created from a Table In MySql, I need to make a HTTP Post to insert the XML into a Web Service. The Web Service just accepts SOAP, HTTP POST and HTTP GET methods. I tried to make the HTTP POST request in different ways with no luck at all. I never worked with SOAP before. How can I make the HTTP POST or SOAP Request?

post_xml.xml:

<?xml version="1.0" encoding="utf-8"?>
<?ADF version="1.0"?>
<adf>
<prospect><id sequence="1" source="xxxs">37</id>
<requestdate>2013-07-10 06:10:42</requestdate>
<vehicle interest="buy" status="new">
<year>2013</year>
<make>12</make>
<model>21</model>
<trim>Sport</trim>
</vehicle>
<customer>
<contact>
<name part="first">Jay</name>
<name part="last">11z</name>
<email>test@gmail.com</email>
<phone time="morning" type="voice" preferredcontact="1">99999999</phone>
<address>
<street line="1">1130 E Test</street>
<city>sa</city>
<regioncode>Z</regioncode>
<postalcode>79924</postalcode>
<country>USA</country>
</address>
</contact>
</prospect>
</adf>

client1.php (HTTP POST CODE)

$xml = file_get_contents('post_xml.xml');
$url = 'http://stg.sa.com/post.asmx/';

$post_data = array ("XML" => $xml);
$stream_options = array(
    $url => array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
        'content' => http_build_query($post_data)
    )
);
$context = stream_context_create($stream_options);    
$response = file_get_contents($url, null, $context);

HTTP POST specs of the Web Service:

The following is a sample HTTP POST request and response.

POST /st.asmx/Post HTTP/1.1
Host: stg.sa.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
XML= string

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0"?>
xml
hakre
  • 193,403
  • 52
  • 435
  • 836
berort media
  • 43
  • 1
  • 1
  • 4
  • Are you sure that XML should be passed in `XML` parameter, not as request body? – dev-null-dweller Jul 10 '13 at 19:05
  • yes, this is the web service post example:http://st.sa.com/ausapost.asmx/Post?XML=8002010-03-18T07:50:53-05:002004MERCEDES-BENZE-ClassE320 Rwd 4dr Sedan (3.2L 6cyl 5A)JohnDoevalid@email.com... – berort media Jul 10 '13 at 19:13

1 Answers1

9

I think your POST array is wrong

Try:

$xml = file_get_contents('post_xml.xml');
$url = 'http://stg.sa.com/post.asmx/';


$post_data = array(
    "xml" => $xml,
);

$stream_options = array(
    'http' => array(
       'method'  => 'POST',
       'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
       'content' => http_build_query($post_data),
    ),
);

$context  = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);
hakre
  • 193,403
  • 52
  • 435
  • 836
Dani
  • 1,220
  • 7
  • 22
  • I change the $URL to 'http' in the array but still have no communication to the web service. – berort media Jul 10 '13 at 19:31
  • Thank you Dan2600, your code is the right way to make the post. My provider sent me a wrong URL. – berort media Jul 10 '13 at 20:14
  • 1
    Note that if the other end is reading this data raw with like php://input, and immediately interpreting it as XML without needing to pull it out of the associated array variable index "xml", then you should replace `http_build_query($post_data)` with simply `$xml`. – Volomike Jan 12 '17 at 03:24