curl_exec($ handle)不返回任何内容

问题描述 投票:0回答:2

我正在用PHP和Curl构建一个简单的CRUD API,并且我有一个HTML表单,用于发送ID,FName,LName。该表单被发布到create.php,然后调用ApiHandler.php来设置Curl请求,然后将其发送到read-api.php。json文件使用新用户更新,并以字符串形式返回json文件内容,但$ response是空字符串,并且不包含json内容$response = curl_exec($handle); // $response = ""

ApiHandler.php

<?php
function callAPI($method, $url, $request){
   $handle = curl_init();
   switch ($method){

      case "GET":
         curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'GET');                        
         curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);    
         break;

      case "PUT":
         curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($request)
            curl_setopt($handle, CURLOPT_POSTFIELDS, $request);                             
         break;

      case "POST":
         curl_setopt($handle, CURLOPT_POST, 1);
         if ($request)
         curl_setopt($handle, CURLOPT_POSTFIELDS, $request);
         break;

       case "DELETE":
         curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "DELETE");  

         default:
         if ($request)
             $handle = sprintf("%s?%s", $url, http_build_query($request));
   }

   curl_setopt($handle, CURLOPT_HTTPHEADER, [
      "Content-Type: application/json; charset=UTF-8",
      "Access-Control-Allow-Origin: *"
      ]);

   curl_setopt($handle, CURLOPT_URL, $url);
   curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);

   $response = curl_exec($handle); // $response = ""??

        $errno = curl_errno($handle);
        $err = curl_error($handle);

  curl_close($handle);

      if ($errno) {
         return "cURL Error #:" . $err;} 
      else {

   return $response;
}


}

function getBaseUrl()
{
   return "http://localhost/api/";
}

?>

这是接受请求并应将其保存到文件并返回json字符串的api。

create-api.php

<?php 
include('ApiHandler.php');

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");


$postData = file_get_contents('php://input');

$data = array();
parse_str($postData, $data);

$json = file_get_contents('employees.json');
$obj = json_decode($postData,true);
$jsonarray = json_decode($json,true);



array_push($jsonarray,$obj);
$result = json_encode($jsonarray);
$file = fopen('employees.json','w');
fwrite($file,$result);
fclose($file);
return $result;

?>

create.php

<?php
include('ApiHandler.php');

$url = getBaseUrl()."create-api.php";

$postdata = file_get_contents("php://input");
$id = $_REQUEST["id"];
$firstname = $_REQUEST["firstname"];
$lastname = $_REQUEST["lastname"];

$arrayRequest = array("id"=>$id,"firstname"=>$firstname,"lastname"=>$lastname);


$json = json_encode($arrayRequest,true);

$output = callAPI("POST", $url, $json);


var_dump($output);

echo ("<p>$output</p>");
?>

有人知道为什么响应为空吗?

php json php-curl
2个回答
1
投票

您的create-api.php脚本需要输出curl_exec()捕获的内容

因此您需要用return $result;替换您的echo $result;(在此情况下这是没有意义的,因为您不在函数中)


0
投票

尝试从以下位置更改代码中的这一行:

curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);

至此:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true)
© www.soinside.com 2019 - 2024. All rights reserved.