我正在开发一个项目,我必须在我们的系统中包含第三方应用程序。在调用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上得到了postet。现在我想使用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 );
不幸的是,这根本不起作用。我正在执行此代码块时没有数据到达。我不知道我做错了什么。谁能告诉我这里的错误是什么? 或者甚至有更好的方法来做到这一点?
答案 0 :(得分:1)
编辑:抱歉 - 我发布了获取,下面是要发布的代码。
这是我使用的代码效果很好:
$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);