dongzhan5943 2013-06-23 08:41
浏览 15
已采纳

ajax php参数为空

I am passing this URL

"url=http://localhost.com/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=C5"

into this php file

<?php
//set POST variables
$url = $_POST['url'];
unset($_POST['url']);

$fields_string = "";
//url-ify the data for the POST
foreach($_POST as $key=>$value) {
    $fields_string .= $key.'='.$value.'&';
}
$fields_string = rtrim($fields_string,'&');

//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($_POST));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>

For some reason it says unknown function f, but when I paste it into a browser manually it works perfectly fine??

  • 写回答

1条回答 默认 最新

  • dongluan0020 2013-06-23 09:29
    关注

    Did you you try urlencode?

    <?php
      //set POST variables
      $url = $_POST['url'];
      unset($_POST['url']);
      $fields_string = "";
      //url-ify the data for the POST
      foreach($_POST as $key=>$value) {
       $fields_string .= $key.'='.urlencode($value).'&';
       }
       rtrim($fields_string,'&');
       //open connection
       $ch = curl_init();
       //set the url, number of POST vars, POST data
       curl_setopt($ch,CURLOPT_URL,$url);
       curl_setopt($ch,CURLOPT_POST,true);
       curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
       //execute post
       $result = curl_exec($ch);
       //close connection
       curl_close($ch);
       ?>
    

    You can try it this way.

     <?php
      //set POST variables
      $url = $_POST['url'];
    
      unset($_POST['url']);
    
      $fields_string = "";
      //url-ify the data for the POST
    
      $ fields_string = http_build_query ($_POST);
       //open connection
       $ch = curl_init();
       //set the url, number of POST vars, POST data
       curl_setopt($ch,CURLOPT_URL,$url);
    
       curl_setopt($ch,CURLOPT_POST,true);
       curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
       //execute post
       $result = curl_exec($ch);
    
       //close connection
       curl_close($ch);
       ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题