[英]how to post variables to file like jquery ajax but with php
我正在一个项目中,我必须在系统中包含第三方应用程序。 调用index.php时,此应用程序使用jQuery ajax将参数传输到PHP文件。 确切的功能如下所示:
$.ajax( {
type : "POST",
cache : false,
crossDomain : false,
async : true,
url : "/somwhere/somefile.php",
data : "msg=37120&hello=true&USRID=1"
)};
我对AJAX并不十分了解,但据我所知,最终"msg=37120&hello=true&USRID=1"
参数在somefile.php上得到了邮戳。 现在,我想使用PHP而不是JS / AJAX将变量直接发布到somefile.php上。
经过一些研究,我发现以下解决方案可以将帖子数据直接发送到somefile.php
:
$url = "/somwhere/somefile.php";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, "msg=37120&hello=true&USRID=1");
$response = curl_exec( $ch );
不幸的是,这根本不起作用。 当我执行此代码块时,没有数据到达。 我不知道我在做什么错。 有人可以告诉我这是什么错误吗? 还是有更好的方法来做到这一点?
编辑:对不起-我发布了get,下面是发布的代码。
这是我使用的代码,效果很好:
$cookie="cookie.txt";
$useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)";
$data=array(
"field1" => 'data1',
"field2" => 'data2',
"SUBMIT" => 'true'
);
$cr = curl_init($url);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true); // Get returned value as string (dont put to screen)
curl_setopt($cr, CURLOPT_USERAGENT, $useragent); // Spoof the user-agent to be the browser that the user is on (and accessing the php $
curl_setopt($cr, CURLOPT_COOKIEJAR, $cookie); // Use cookie.txt for STORING cookies
curl_setopt($cr, CURLOPT_COOKIEFILE, $cookie); // Use cookie.txt for STORING cookies
curl_setopt($cr, CURLOPT_POST, true); // Tell curl that we are posting data
curl_setopt($cr, CURLOPT_POSTFIELDS, $data); // Post the data in the array above
$output = curl_exec($cr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.