0

I have 2 websites (2 different domains/servers) and i need to transfer data between them. Source Server will be having an Array/ Array Object. Then it needs to be delivered to another site. I do not want the original Array to be malformed.

What i can think so far is, to use cURL from Destination Server and then, provide the Array Object as echo json_encode( $array ) from Source Server.

Will it work please? It is actually most likely a Web Service but i just don't know how to approach. What is your best suggestion please?

Note: Actually i do not have any restriction on the methods, or direction as well. Whether json_encode or cURL or whatever can be suggested, as long as the data reach to destination server. Thanks so much!

夏期劇場
  • 17,821
  • 44
  • 135
  • 217
  • It will work. But it's not possible to evaluate a current solution or propose something "better" until you provide some criterias to us (obviously you cannot compare something until you know how). – zerkms May 09 '13 at 04:39

3 Answers3

1

use http_build_query with CURL for sending big arrays.

Edit

Example

$str = http_build_query($array);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "my_url");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);  // $array is my above data

Post multidimensional array using CURL and get the result on server

Community
  • 1
  • 1
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • So you mean all above codes are for `Source Server` which is having the array, please? How to receive from `Destination Server` pls? – 夏期劇場 May 09 '13 at 04:44
  • 1
    @夏期劇場 Get the result at server end using `$_POST`. – Yogesh Suthar May 09 '13 at 04:49
  • So you mean, the method/script above is for "data source" server, is it? It will "send/push" the data into "destination / consumer" server. Then the destination server have to accept the data by using `$_POST[]` is it? – 夏期劇場 May 09 '13 at 07:06
1

Send an HTTP POST request via cURL functions and add the serialize() array to the request body.

Serialize function are useful check here

Community
  • 1
  • 1
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • OP wanted to use `json_encode`. Any reason to use `serialize` instead? – zerkms May 09 '13 at 04:39
  • HTTP POST via cURL (from Destination Server) and serialize (from Host Server) you mean please? – 夏期劇場 May 09 '13 at 04:40
  • Yes it useful check here http://stackoverflow.com/questions/1306740/json-vs-serialized-array-in-database – chandresh_cool May 09 '13 at 04:40
  • Hi @zerkms Actually i do not have any restriction on the methods. Whether `json_encode` or `cURL` or whatever can be suggested. Thanks!! – 夏期劇場 May 09 '13 at 04:42
  • @夏期劇場: as soon as `serialize` is a php-only format while `json` is platform-agnostic - you may decide yourself which is "better" – zerkms May 09 '13 at 04:44
1

Does the direction of the flow matters? If not, make it so that the consumer requests the data rather than server send the data to the consumer, this way there is no POST restrictions etc. Also, it can be a simple json response. So the consumer request a URL yourserver.com/data/customers.php Which will then simply do a json_encode($array);

EDIT so that the code example is clear Following is the end point side of things

endpoint.php

#big array
$array = array('Apple','Banana');

header('Content-Type: application/json');
echo json_encode($array);
exit;

//EOF

And the consumer

$json_obj = json_decode(file_get_contents('http://yourdomain.com/endpoint.php')); 
print_r($json_obj);
Community
  • 1
  • 1
xelber
  • 4,197
  • 3
  • 25
  • 33
  • Yes, no restriction. Either way as long as we can transfer the data. So you mean to send a normal POST request from pulling Server? With cURL? – 夏期劇場 May 09 '13 at 06:05
  • 1
    No need to be a POST, simple url call.. $json_ob = json_decode(file_get_contents('http://yourdomain.com/urltofile.php')); urltofile.php should dump the array as json object (i.e. json_encode($array); ) Basically http://yourdomain.com/urltofile.php becomes an endpoint. – xelber May 09 '13 at 06:09
  • woah! this is really great!!! btw, is there any security methods we can apply? i mean, of course this is another topic but is there any way to protect the `endpoint.php` page contents? as you know we do not want everyone to be accessing this data. – 夏期劇場 May 09 '13 at 06:40
  • Security depends on the level you are expecting, at the lowest, you could just pass in a user name pwd combination over the URL, so for example endpoint.php?user=[User]&password=[Password] If you do this over SSL, more than enough for your requirement I suppose. You can also use HTTP Basic Auth, or embed a user name and pwd in the request, Which will mean you have to use cURL. – xelber May 09 '13 at 06:59