Retrieving XML with cURL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bgm
Forum Newbie
Posts: 2
Joined: Thu Mar 10, 2011 7:04 am

Retrieving XML with cURL

Post by bgm »

Hi there

I am trying to use cURL to retrieve XML from a gateway. The code I am using works (ie. returns the HTTP response) for any URL that I use except the one that I need! The URL is below, and when using that I get a blank response every time. I've tested and can telnet to that IP address on that port from my server.

I'm not sure if it is because of the format/structure of the URL? Could it have something to do with the port of the gateway or the whay the parameters are being sent? Any assistance would be much appreciated. The URL I am accessing, and the code are both below:

URL
http://196.11.120.190:8080/mtnusa/clien ... nd=<usareq NODE="tHTTP" USERNAME="HTTP" PASSWORD="1234" TRANSFORM="SMPP"><command><submit_sm><a_number>278200703520709</a_number><b_number>27824411926</b_number><service_type/><message>Test</message><registered_delivery/></submit_sm></command></usareq>

CODE
The function I am using (from http://davidwalsh.name) is:

Code: Select all

/* gets the data from a URL */
function get_data($url)
{
   $ch = curl_init();
   $timeout = 5;
   curl_setopt($ch,CURLOPT_URL,$url);
   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
   $data = curl_exec($ch);
   curl_close($ch);
   return $data;
}

$returned_content = get_data('http://196.11.120.190:8080/mtnusa/client.jsp?command=<usareq NODE="HTTP" USERNAME="HTTP" PASSWORD="1234" TRANSFORM="SMPP"><command><submit_sm><a_number>278200703520709</a_number><b_number>27824411926</b_number><service_type/><message>Test</message><registered_delivery/></submit_sm></command></usareq>');
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Retrieving XML with cURL

Post by mecha_godzilla »

That URL certainly looks a bit odd and it doesn't look like a properly formed GET request - it looks like the JSP script expects a single GET value and this value then contains all the other values (structured in XML format). I don't use JavaServerPages so I could easily be wrong here but it seems odd that the server would be expecting the query to be sent in XML format without any kind of URL encoding. Alternatively, you might need to send this information to the script as a POST request, in which case you don't want to include it in the URL but need to send it as a set of POST values (which cURL supports).

HTH,

Mecha Godzilla
Post Reply