php通过curl传输json数据到另外一个php并在那个界面显示

最近在写一个功能,需要使用到curl发送到json数据某个地址上并进行显示,发送的页面代码若下

$ch = curl_init();
$data = ['username' => 'admin', 'password' => '123456'];
$url = 'http://www.csl.com/program/ENG/course_new/approveleaveabc.php';
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//设置链接

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//POST数据
// curl_setopt($ch, CURLOPT_HTTPHEADER, array( 
// 'Content-Type: application/json',
// 'Content-Length: ' . strlen($data))
// );
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
    print curl_error($ch);
}
curl_close($ch); //关闭curl链接
// echo $response;//显示返回信息 
print_r($response);

然后接受界面代码

$res = file_get_contents('php://input');
print_r($res);
print_r($_POST);

最后的结果是有返回值,但是接受界面还是空的就如下图(第一张是接受界面,第二张是发送的),这是什么原因呢?
img

img

request()->params();
$_GET 或 $_POST 试下

另外这一句是错的 。
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//POST数据
应该 是json_encode($data),你这个写法是get请求

参考:


protected function PostCurlRequest($remote_server, $post_string)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

$res = file_get_contents('php://input'); 获取的可能是字符串

$ch = curl_init();
$data = ['username' => 'admin', 'password' => '123456'];
$url = ' http://www.csl.com/program/ENG/course_new/approveleaveabc.php' http://www.csl.com/program/ENG/course_new/approveleaveabc.php' ;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//设置链接

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $data ));
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//POST数据
// curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// 'Content-Type: application/json',
// 'Content-Length: ' . strlen($data))
// );
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
print curl_error($ch);
}
curl_close($ch); //关闭curl链接
// echo $response;//显示返回信息
print_r($response);

www.csl.com和csl.com这两个不对等哦 http://www.csl.xn--comcsl-k76j.xn--com-928d4dnb771h6xnjv2e470b/

程序没问题吧。

发送程序 最后有数据返回,说明 接收程序 正常的。

单独访问 接收程序 肯定没有数据的

可以找我

你可以error_log

说到curl,推荐大家用一款支持curl导入接口的接口工具ApiPost,特别方便,基本满足工作需要,快捷开发