发布请求可以在Postman中工作,但不能在php代码中工作(SSL_ERROR_SYSCALL)

时间:2019-07-24 08:24:18

标签: php curl post request postman

我正在尝试使用Postman的PHP代码片段进行简单的curl请求。但是,当我尝试运行它时,出现以下错误:“ OpenSSL SSL_read:SSL_ERROR_SYSCALL,errno 10054”

在邮递员中,请求运行正常,我确实得到了json结果。 这是邮递员头文件Postman Header

的嵌入式链接

和邮递员尸体Postman body

我的php代码如下:


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://www.oglasnik.hr/ajax/getNearBy",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 500,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"classifiedId\"\r\n\r\n3996266\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"bounds\"\r\n\r\n{\"east\":16.00793838500977,\"west\":15.977039337158205,\"north\":45.78652940999985,\"south\":45.773958941535355}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
            CURLOPT_HTTPHEADER => array(
                "Accept: application/json, text/javascript, */*; q=0.01",
                "Accept-Encoding: gzip, deflate, br",
                "Accept-Language: en-US,en;q=0.9",
                "Cache-Control: no-cache",
                "Connection: keep-alive",
                "Content-Length: 384",
                "Content-Type: application/x-www-form-urlencoded",
                "Cookie: cookieconsent_status=allow; ogl_visits^[0^]=a5cbwGRWU7YEC1LP6tKOVOzf6GIZzyU7dbry64BfNak4xygExIAj^%^2FkGWQ5azKqmznEHJVCF4ZfufdAaCugfPjPi8vX7CB8Gs^%^2F3OdX5RVvYjyfWKHUHboChkYfbURk6Ly",
                "Host: www.oglasnik.hr",
                "Origin: https://www.oglasnik.hr",
                "Postman-Token: 60eebbd3-ed26-4aed-ae32-f190353ce333,aa32ad1d-55c6-4fa6-928e-3309a497c36a",
                "Referer: https://www.oglasnik.hr/stanovi-najam/novi-zagreb-bundek-2-sobni-stan-55-m2-oglas-3996266",
                "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
                "X-Requested-With: XMLHttpRequest",
                "cache-control: no-cache",
                "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }

更新

使用@Andrew的技巧,我将CURLOPT_HTTPHEADER更改为:

 CURLOPT_HTTPHEADER => array(
              "Accept: application/json, text/javascript, */*; q=0.01",
              "Accept-Encoding: gzip, deflate, br",
              "Accept-Language: en-US,en;q=0.9",
              "Cache-Control: no-cache",
              "Connection: keep-alive",
              "Content-Type: application/x-www-form-urlencoded",
              "Cookie: cookieconsent_status=allow; ogl_visits^[0^]=a5cbwGRWU7YEC1LP6tKOVOzf6GIZzyU7dbry64BfNak4xygExIAj^%^2FkGWQ5azKqmznEHJVCF4ZfufdAaCugfPjPi8vX7CB8Gs^%^2F3OdX5RVvYjyfWKHUHboChkYfbURk6Ly",
              "Host: www.oglasnik.hr",
              "Origin: https://www.oglasnik.hr",
              "Referer: https://www.oglasnik.hr/stanovi-najam/novi-zagreb-bundek-2-sobni-stan-55-m2-oglas-3996266",
              "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
              "X-Requested-With: XMLHttpRequest",
              "cache-control: no-cache",
              "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
            )

我得到了回应!不幸的是,它唯一

{
    "status": false
}

看起来我发送的post参数错误。问题是我需要发送一个整数参数和一个字符串作为json,目前正在尝试这样做:

 $bounds = 
  [
     "east" => 16.00793838500977,
      "west" =>15.977039337158205,
      "north" =>45.78652940999985,
      "south" =>45.773958941535355
  ];


  $params = [
      "classifiedId" => 3996266, "bounds"  => json_encode($bounds)
   ];

并在curl_setopt_array中添加此参数:

 CURLOPT_POSTFIELDS => $params

最终更新:

成功了!正如@Andrew在评论中指出的,它可以使用:

  CURLOPT_POSTFIELDS => http_build_query($params)

所以最终的标头是:

 curl_setopt_array($curl, array(
            CURLOPT_URL => "https://www.oglasnik.hr/ajax/getNearBy",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => http_build_query($params),
            CURLINFO_HEADER_OUT =>  true,
            CURLOPT_HTTPHEADER => array(
              "Accept: application/json, text/javascript, */*; q=0.01",
              "Accept-Encoding: gzip, deflate, br",
              "Accept-Language: en-US,en;q=0.9",
              "Cache-Control: no-cache",
              "Connection: keep-alive",
              "Content-Type: application/x-www-form-urlencoded",
              "Cookie: cookieconsent_status=allow; ogl_visits^[0^]=a5cbwGRWU7YEC1LP6tKOVOzf6GIZzyU7dbry64BfNak4xygExIAj^%^2FkGWQ5azKqmznEHJVCF4ZfufdAaCugfPjPi8vX7CB8Gs^%^2F3OdX5RVvYjyfWKHUHboChkYfbURk6Ly",
              "Host: www.oglasnik.hr",
              "Origin: https://www.oglasnik.hr",
              "Referer: https://www.oglasnik.hr/stanovi-najam/novi-zagreb-bundek-2-sobni-stan-55-m2-oglas-3996266",
              "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
              "X-Requested-With: XMLHttpRequest",
              "cache-control: no-cache",
              "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
            ),
          ));

0 个答案:

没有答案