【问题标题】:The api request gets the correct answer via postman, but the curl request in the server side receives a null responseapi请求通过邮递员得到正确答案,但是服务端的curl请求收到的是空响应
【发布时间】:2019-05-21 02:36:27
【问题描述】:

这是我的代码的 3 部分:

  • “a”部分是我的 curl 请求正文。

  • “b”部分是我的网站后端,其中包含一个 curl 请求。

  • “c”部分是 postman 中此请求的结果。

我没有收到任何错误,但我收到了一个空响应。您能否告诉我在浏览器中收到空响应是什么问题?

第一部分:----------------------------------

class API
{

    /**
    * @var string
     */

    private $ip = "http://localhost:5000";

    /**
     * @var string
     */
    private $baseUrl = "/M-Pages/Api";
    private $port;
    private $method;
    private $header;
    private $body;
    private $rout;
    private $curl;

    /**
     * API constructor.
     */
    public function __construct()
    {
        $this->curl = curl_init();
    }

    /**
     * @param $rout
     * @param $method
     * @param null $port
     * @param array $header
     * @param array $body
     */
    public function curlRequest($rout , $method = null , $port = null,$header = ["content-type: application/json"] , $body = [])
    {


        // set parameter of request
        $this->initRequestParameters($rout, $method, $port, $header, $body);

        // set option of request
        $this->initCurlOptions();

        // execute curl request and get result
        $response = curl_exec($this->curl);

        // get error of curl request
        $err = curl_error($this->curl);

        // close curl

        // check error of request
        if ($err) {
            //TODO :error change

            echo "CURL Error #:" . $err;//TODO: set custom error function
        } else {
            return $response;
            curl_close($this->curl);
       }
    }

    /**
     * @param $rout
     * @param $method
     * @param null $port
     * @param array $header
     * @param array $body
     */
    private function initRequestParameters($rout , $method , $port = null, $header = ["content-type: application/json"] , $body = []){

        $this->port = $port != null ? $port : "5000";
//        var_dump( $this->port );
        $this->method = $method != null ? strtoupper($method) : "GET";
//        var_dump( $this->method);
        $this->header = $header != [] ? $header :["content-type: application/json"];
//        var_dump($this->header);
        $this->body = $body != [] ? $body : [];
//        var_dump($this->body);
        $this->rout = $rout;
//        var_dump( $this->rout);
    }

    /**
     * @return resource
     */
    private function initCurlOptions()
    {

        switch ($this->method) {
            case "POST":
                $this->initPostRequestOption();
                break;
            case "GET":
                $this->initGetRequestOption();
                break;
        }

        return $this->curl;

    }

    private function initPostRequestOption()
    {

        curl_setopt_array($this->curl, array(CURLOPT_PORT => $this->port,
                CURLOPT_URL => $this->ip . $this->port . $this->baseUrl . $this->rout,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => $this->method,
                CURLOPT_HTTPHEADER => $this->header)
        );

//        var_dump( $this->ip . $this->port . $this->baseUrl . $this->rout);

    }
   private function initGetRequestOption()
    {
        curl_setopt_array($this->curl, array(CURLOPT_PORT => $this->port,
                CURLOPT_URL => $this->ip . $this->port . $this->baseUrl . $this->rout,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => $this->method,
                CURLOPT_HTTPHEADER => $this->header)
        );
    }

} 
part b:----------------------------
 $body = "{\n \"params\" : {\"user_name\":\"".$user_name . "\"}}";
//                var_dump($body);
                $this->api = \DI::mapClass("API", "API", $GLOBALS["namespace"]["service"]);
//                echo 'api FIRST';
                $res = $this->api->curlRequest("/First/login","POST",[],[], $body);
                $res = json_decode($res, true);
                var_dump($res);
                exit();

c部分:----这是我的邮递员环境和我的请求的响应:

【问题讨论】:

  • $res 之前 json_decode 是什么?如果json_decode无法解码,则返回null。
  • $res 是 curl 请求的 $response 。那是 null ,但是为什么?它在邮递员中工作,整个步骤都是 true.mr。 @JonStirling
  • 你覆盖了$res$res = $this->api->curlRequest("/First/login","POST",[],[], $body); $res = json_decode($res, true); 那么你说哪个是空的?
  • 两者都是 null @JonStirling
  • 一个长镜头,但我有浏览器扩展/插件吃掉我的 json/xml 数据并显示为空。也许尝试通过 devtools 进行监控,尝试另一个 vanilla 浏览器或禁用所有扩展。

标签: php json api postman


【解决方案1】:

我不是 PHP 人,所以这个答案可能完全错误,但您似乎正在使用 $this->api = \DI::mapClass("API", "API", $GLOBALS["namespace"]["service"]); 将服务映射到特定 API 我知道您有类 API,但在您发送的邮递员中Api/First/login 的 POST(注意大写),这也反映在您的代码中。这可能是使您的 curl 请求无效的错字吗?那行应该是$this->api = \DI::mapClass("API", "Api", $GLOBALS["namespace"]["service"]);吗?

【讨论】:

  • 这一行$this->api = \DI::mapClass("API", "Api", $GLOBALS["namespace"]["service"])等于 ($this->api=new Api();),在构建 api 类之后,调用方法 curlRequest 并将参数传递给它。 @Davenporten
  • curlRequest 应该返回一些东西吗? @法蒂玛
  • 是的,它应该返回 { "status": 200, "response": { "doc_fname": "فاطمه", "doc_lname": "ترکی", "doc_pass": "$2a $10$f4e828d7376550ef12aa0u6p2/Yo0Lh91zeNHJa56ZwCykuQPbwTG" } 但浏览器中的响应为空!!!! @Davenporten
  • 我的意思是实际功能。 if($err) 的 else 中有一个 return 语句,但如果是错误则没有。您可能只是错过了回声,但请尝试返回 $err 并查看您得到的结果。 @法蒂玛
猜你喜欢
  • 2015-11-21
  • 2017-12-06
  • 2019-12-13
  • 1970-01-01
  • 2019-06-09
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多