dtg25862 2013-07-11 14:05
浏览 49

如何将变量发布到文件,如jquery ajax但使用php

I'm working on a project where I have to include a third party application in our system. This application transmitts parameters to a PHP file using jQuery ajax when calling the index.php. The exact function looks like this:

$.ajax( {
    type : "POST",
    cache : false,
    crossDomain : false,
    async : true,
    url : "/somwhere/somefile.php",
    data : "msg=37120&hello=true&USRID=1"
    )};

I'm am not very experienced with AJAX at all but as far as I can see in the end the parameters "msg=37120&hello=true&USRID=1" get postet on somefile.php. Now I want to post the variables directly onto somefile.php using PHP instead of JS/AJAX.

After some researches I found the following solution to send post data directly to the 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 );

Unfortunately this is not working at all. No data arrives when I'm executing this code block. I've no idea what I'm doing wrong. Can someone tell me whats the mistake here? Or is there even a better way how to do this?

  • 写回答

1条回答 默认 最新

  • doupingzhi9674 2013-07-11 14:13
    关注

    Edit: Sorry -- I posted the get, below is the code to post.

    This is the code that I use that works well:

    $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); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线