0
$url='https://example.com/demo/webservice/register?auth_key=cghjcgfchai1723y12y321hjvbv1asdas';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$posted_data);
$response  = curl_exec($ch);

if($response === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    $my_file = 'file.txt';
    $handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
    fwrite($handle,$response);
}
curl_close($ch);
return $response;

regiter.php

public function register()
{
    $key='cghjcgfchai1723y12y321hjvbv1asdas';
    $secretKey=$this->input->get('auth_key');
    if($key!=$secretKey)
    {
        echo json_encode(array("response"=>"error","result"=>array("message"=>"Invalid key ")));
        exit;
    }
    print_r($_POST);
    exit();
}

file.txt

Array
(
    [email] => testn@gmail.com
    [std_name] => testing
    [menu-590] => 1000SK
    [sex] => MALE
    [father_name] => xyz
    [mother_name] => abc
    [dob] => 2020-02-21
    [phone] => 9876543211
    [address] => GHAZIABAD
    [pin] => 201206
    [aadhar] => 123456789012
)

I am using curl to return response from file.txt file to register file where I am simply used print_r($_POST) to know the response is coming or not but it show me blank array. I don't know why? Please help me.

Thank You

Avi
  • 35
  • 6

2 Answers2

0

In your cURL request you are not prinitng anything you are simply returning the response. change that to print

return $response;

In the register function instead of printing return the result.

public function register()
{
    $key='cghjcgfchai1723y12y321hjvbv1asdas';
    $secretKey=$this->input->get('auth_key');
    if($key!=$secretKey)
    {
        echo json_encode(array("response"=>"error","result"=>array("message"=>"Invalid key ")));
        exit;
    }
    else
   {
      $content = trim(file_get_contents("php://input"));
      return $content
   }
}

In the above register function if the key is wrong you will error response, if it is correct then you will receive data which was sent

0

I copied your code and tested in my codeigniter project. I was getting same blank screen or string(0) "". Later I found that some code in the constructer was just redirecting my script based on session value. Try if that is the case in your code too.

Ganesh Patil
  • 36
  • 1
  • 6