Forum Moderators: coopster

Message Too Old, No Replies

return array in php curl response

curl, array, response, php

         

nauman76

11:30 am on Mar 22, 2011 (gmt 0)

10+ Year Member



I have written a small code to send a request using curl. following is an example.

request.php
====================

$ret = "sent data";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/rest2/use.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

print_r($result); // print the returned array from use.php

Now the page which receives the curl response, it has only one line
use.php
====================
return array("fn"=>"first name", "ln"=>"last name");

but in my request.php I am unable to get the array and print it.

How I can pass an array of values from use.php to request.php?

Help is appreciated.

cheers

omoutop

11:48 am on Mar 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



two ideas on what your use.php should echo/print:
- json encode data
- xml data

nauman76

11:52 am on Mar 22, 2011 (gmt 0)

10+ Year Member



@omoutop, how to return xml data, any example please?

And when I echo the array it echos at my request.php but as a string and not an array. any solution to this?

omoutop

7:37 am on Mar 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



example of what use.php should echo:


<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Now your $result variable will hold xml-format data.
You can read them with an xml library/parser (like simpleXML [php.net] for php 5.2)
If you have php <5.2 you must find/write a php class to parse xml data