0

I use cURL but untill now I used it for requesting data from servers. But now I want ot write API and data will be requested with cURL. But I don't know how Server reads data from cURL request.

This is my "client server" side request:

function sendRequest($site_name,$send_xml,$header_type=array('Content-Type: text/xml')) 
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$site_name);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$send_xml);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header_type);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec($ch);
return $result;
}


$xml = "<request>
<session>
 <user>exampleuser</user>
 <pass>examplepass</pass>
</session>
</request>";
$sendreq = sendRequest("http://sitename.com/example.php",$xml);
echo $sendreq;

How do I need to write "main server" side script so I can read what user and pass from request are??? Thank you a lot.

mandza
  • 330
  • 9
  • 24
  • 1
    PHP is executed on the server... this makes no sense. – Digital Chris Jan 13 '14 at 00:44
  • @DigitalChris when i say client it is "client server" that run aplication and server is "Main server" – mandza Jan 13 '14 at 00:45
  • If you must use XML then you need an xml parser in example.php. If your more flexible use HTTP post requests instead to avoid the overhead of an XML parser. – Andy Gee Jan 13 '14 at 00:46
  • @AndyGee I need to use XML becouse request data can be too long. Also I can parse request data but I just dont know how can I read it. $_POST[] or $_SERVER[] or what ever is used for it. – mandza Jan 13 '14 at 00:47
  • For how to parse XML, see [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/q/3577641) – Pekka Jan 13 '14 at 00:48
  • Well XML will make it a whole lot longer. The same request could literally be just an array of key/values or a multidimentional array – Andy Gee Jan 13 '14 at 00:49
  • @Pekka웃 thank you. I am allready using simplexml for xml parsing. but as I sayed I don't know how to read data from request at the first place. after reading it parsing is easy. – mandza Jan 13 '14 at 00:51

1 Answers1

0

To just be able to read it try this

curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=>$send_xml));

Then

print_r($_POST['data'])

Alternatively skip the XML and try something like this:

$data = array(
    'request' => array(
        'session' => array(
            'user'=>'exampleuser',
            'pass'=>'examplepass')
        )
    );


$sendreq = sendRequest("http://sitename.com/example.php",$data);

In example.php

print_r($_POST)
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
  • this is my output for 2 example in your answer @Andy Gee Array ( ) for the first one i don't have output at all – mandza Jan 13 '14 at 01:00