Hello I am passing an JSON array from one server say www.example1.com and I want to receive that data on another server say www.example2.com/test.php . I have tried this using cURL but I am not getting that data at the receiving. Following is my code
您好我从一台服务器传递一个JSON数组,例如www.example1.com,我希望在另一台服务器上接收该数据,例如www.example2.com/test.php。我已经尝试使用cURL,但我没有在接收时获得该数据。以下是我的代码
Code at Sender
发件人代码
$send_data = json_encode($myarray);
$request_url = 'www.example2.com/test.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'send_data='.$send_data);
$response = curl_exec($curl);
$curl_error = curl_error($curl);
curl_close($curl);
Code at Receiver
接收方代码
if(isset($_REQUEST['send_data'])){
$userinfo = json_decode($_REQUEST['send_data'],true);
print_r($userinfo);
}
How do I fetch the data at receiver's end.
如何在接收端获取数据。
2 个解决方案
#1
1
Try this method.
试试这个方法。
FILE: example1.com/sender.php
$request_url = 'www.example2.com/test.php';
$curl = curl_init( $request_url );
# Setup request to send json via POST.
$send_data = json_encode($myarray);
curl_setopt( $curl, CURLOPT_POSTFIELDS, $send_data );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($curl);
curl_close($curl);
# Print response.
echo "<pre>$result</pre>";
on your second page, you can catch the incoming request using file_get_contents("example1.com/sender.php"), which will contain the POSTed json. To view the received data in a more readable format, try this:
在第二页上,您可以使用file_get_contents(“example1.com/sender.php”)捕获传入的请求,其中包含POSTed json。要以更易读的格式查看收到的数据,请尝试以下操作:
echo '<pre>'.print_r(json_decode(file_get_contents("example1.com/sender.php")),1).'</pre>';
#2
0
Use the following
使用以下内容
FILE: example1.com/sender.php
<?php
header('Content-Type: application/json'); echo
json_encode(array('response1' => 'This is response1', 'response2' => 'This is response2', $_POST));
?>
FILE: example2.com/receiver.php
<?php
$request_url = 'http://www.example1.com/sender.php';
$sendData = array('postVar1' => 'postVar1');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'sendData=' . http_build_query($sendData));
print_r($response = curl_exec($curl));
curl_close($curl);
?>
You will get a JSON object as a cURL response.
您将获得一个JSON对象作为cURL响应。
#1
1
Try this method.
试试这个方法。
FILE: example1.com/sender.php
$request_url = 'www.example2.com/test.php';
$curl = curl_init( $request_url );
# Setup request to send json via POST.
$send_data = json_encode($myarray);
curl_setopt( $curl, CURLOPT_POSTFIELDS, $send_data );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($curl);
curl_close($curl);
# Print response.
echo "<pre>$result</pre>";
on your second page, you can catch the incoming request using file_get_contents("example1.com/sender.php"), which will contain the POSTed json. To view the received data in a more readable format, try this:
在第二页上,您可以使用file_get_contents(“example1.com/sender.php”)捕获传入的请求,其中包含POSTed json。要以更易读的格式查看收到的数据,请尝试以下操作:
echo '<pre>'.print_r(json_decode(file_get_contents("example1.com/sender.php")),1).'</pre>';
#2
0
Use the following
使用以下内容
FILE: example1.com/sender.php
<?php
header('Content-Type: application/json'); echo
json_encode(array('response1' => 'This is response1', 'response2' => 'This is response2', $_POST));
?>
FILE: example2.com/receiver.php
<?php
$request_url = 'http://www.example1.com/sender.php';
$sendData = array('postVar1' => 'postVar1');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'sendData=' . http_build_query($sendData));
print_r($response = curl_exec($curl));
curl_close($curl);
?>
You will get a JSON object as a cURL response.
您将获得一个JSON对象作为cURL响应。