Jason Millington
Jason Millington

Reputation: 691

ajax php parameter empty

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

Upvotes: 0

Views: 101

Answers (1)

Panama Jack
Panama Jack

Reputation: 24478

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

Upvotes: 1

Related Questions