Php 从一台服务器发送JSON,在另一台服务器接收

Php 从一台服务器发送JSON,在另一台服务器接收,php,json,curl,Php,Json,Curl,您好,我正在从一台服务器(如www.example1.com)传递一个JSON数组,我希望在另一台服务器(如www.example2.com/test.php)上接收该数据。我已经用cURL尝试过了,但是在接收端我没有得到数据。下面是我的代码 发件人处的代码 $send_data = json_encode($myarray); $request_url = 'www.example2.com/test.php'; $curl = curl_init(); curl_s

您好,我正在从一台服务器(如www.example1.com)传递一个JSON数组,我希望在另一台服务器(如www.example2.com/test.php)上接收该数据。我已经用cURL尝试过了,但是在接收端我没有得到数据。下面是我的代码

发件人处的代码

$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); 
if(isset($_REQUEST['send_data'])){
    $userinfo = json_decode($_REQUEST['send_data'],true);
    print_r($userinfo);
}
接收器处的代码

$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); 
if(isset($_REQUEST['send_data'])){
    $userinfo = json_decode($_REQUEST['send_data'],true);
    print_r($userinfo);
}
如何在接收器端提取数据。

使用以下方法

文件:example1.com/sender.php

<?php
header('Content-Type: application/json'); echo
json_encode(array('response1' => 'This is response1', 'response2' => 'This is response2', $_POST));
?>
$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>";

文件: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);
?>

您将得到一个JSON对象作为cURL响应。

试试这个方法

文件:example1.com/sender.php

<?php
header('Content-Type: application/json'); echo
json_encode(array('response1' => 'This is response1', 'response2' => 'This is response2', $_POST));
?>
$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>";
$request_url='www.example2.com/test.php';
$curl=curl\u init($request\u url);
#安装程序请求通过POST发送json。
$send_data=json_encode($myarray);
curl_setopt($curl,CURLOPT_POSTFIELDS,$send_data);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/json'));
#返回响应而不是打印。
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
#发送请求。
$result=curl\u exec($curl);
curl_close($curl);
#打印响应。
回显“$result”;
在第二个页面上,您可以使用包含发布的json的文件\u get\u contents(“example1.com/sender.php”)捕获传入请求。要以更可读的格式查看收到的数据,请尝试以下操作:

echo''。打印(json解码(文件获取内容(“example1.com/sender.php”)),1.'';

尝试回显
$response
您应该执行上述操作^echo$response在接收方以1添加打印($请求)的形式向我提供输出,或者您可以通过电子邮件将其发送给您,以便您知道在接收方收到的请求