/    Sign up×
Community /Pin to ProfileBookmark

Retuning XML data via cURL!

Hi all,

I’d like to know how to return data via cURL.

I have the following scenario:
Site A sends an XML file via cURL to site B. Site B does some processing on the data and returns some data back to site A.

I understand how to create and send the XML from site A, and I understand how to receive, parse and process the XML on site B – what I don’t understand is how to return the results of the processing on site B to site A!

I think it has something to do with…

[code=php]$result = curl_exec($ch);[/code]

…when site A cURL’s the XML – could someone please tell me how this works please?

Thanks!

dai.hop

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@SyCoApr 22.2009 — Have a read up in the manual at php.net for cURL. There's lots of examples. Here's one that returns the source of a page as a string. cnage the URL and parse it however you need to.

[code=php]$url = "http://www.whatever.com";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
$result = curl_exec($ch); // run the whole process
curl_close($ch);

echo $result;[/code]
Copy linkTweet thisAlerts:
@dai_hopauthorApr 23.2009 — Thanks for the reply.

In my example the code you've posted would be on site A - correct?
Copy linkTweet thisAlerts:
@the-ferretApr 23.2009 — Hi all,

Site A sends an XML file via cURL to site B. Site B does some processing on the data and returns some data back to site A.

dai.hop[/QUOTE]


maybe a numpty comment, but isn't that what SOAP does ... calls a remote procedure, delivering an XML payload.
Copy linkTweet thisAlerts:
@criterion9Apr 23.2009 — Check out the manual:

http://us3.php.net/curl
Copy linkTweet thisAlerts:
@dai_hopauthorApr 28.2009 — I've already spent time reading the manual, this is my second port of call. My curl script looks like this:

[code=php]function curl($xml, $dest)
{
$url = 'http://www.examplesite.com/api/';
$url .= $dest . '.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}[/code]


I've turned on "CURLOPT_RETURNTRANSFER" and am calling the function like this:

[code=php]$result = curl($xml, 'file');
echo '<p>Result: ' . $result . '</p>';[/code]


http://www.examplesite.com/api/file.php simply contains the following:

[code=php]echo 'TEST!';[/code]

I'm not getting anything back! Does anyone know what part is incorrect?
Copy linkTweet thisAlerts:
@SyCoApr 28.2009 — Site A sends an XML file via cURL to site B. Site B does some processing on the data and returns some data back to site A.[/QUOTE]

This would work all from site B as that's the site doing the processing. Site A just serves the page and receives the cURL post. So Site A doesn't 'send' the page, think more like site B gets the page, processes and sends back to site A.

So a script on site B opens a URL on site A with fopen() or file() or whatever you need to use.

It 'does some processing' and cURL posts back to a URL on site A. It can be a different URL if you want or the same one. The script at that URL on Site A receives the cURL POST like any form landing page would.
×

Success!

Help @dai_hop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...