如何使用codeigniter将响应从文本文件返回到webapi


$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
)

我使用curl将响应从file.txt文件返回到register文件,在那里我只使用print_r($_POST)来知道响应是否到来,但它向我显示空白数组。我不知道为什么?请帮帮我。

谢谢

在您的cURL请求中,您并没有得到任何东西,只是返回响应。将其更改为打印

return $response;

在寄存器函数中不打印返回结果。

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

在上面的寄存器功能中,如果密钥是错误的,您将错误响应,如果它是正确的,那么您将接收发送的数据

我复制了您的代码,并在我的codeigniter项目中进行了测试。我得到了相同的空白屏幕或string(0(">。后来我发现构造器中的一些代码只是根据会话值重定向我的脚本。如果代码中也存在这种情况,请尝试。

最新更新