0

我正在传递这个 URL

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

进入这个 php 文件

<?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);
?>

出于某种原因,它说未知功能 f,但是当我手动将其粘贴到浏览器中时,它工作得很好?

4

1 回答 1

1

你试过了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);
   ?>

你可以这样试试。

 <?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);
   ?>
于 2013-06-23T09:29:37.253 回答