首页 > 解决方案 > 如何修复由 JSON 内容中的负数引起的 php file_get_contents 警告?

问题描述

我正在尝试使用 file_get_contents() 使用 Zoho 的 COQL API(https://www.zoho.com/crm/developer/docs/api/Get-Records-through-COQL-Query.html)执行 SQL 查询获取经纬度范围内的客户列表。当我传递我的 SQL 语句时,file_get_contents()无法打开流。

我已经在两个小时的大部分时间里一直在解决这个问题,据我所知,这个问题只有在查询中出现负数时才会出现。如果我删除连字符,我可以执行成功的查询并从服务器获得响应。我不明白发生了什么或如何解决它。

为了隐私,我已经更改了经度和纬度数字,否则这是我正在使用的代码。


$query = "select Last_Name, First_Name, Mailing_Street, Mailing_City, Mailing_State, Mailing_Zip, Latitude, Longitude
      from Contacts
      where (Latitude between 29.831755 and 29.889647)
      and (Longitude between -114.994548 and -115.069968)";

private function perform_query(){
        $data = json_encode(["select_query" => $this->query]);
        $result = file_get_contents($this->config['coql_url'], FALSE, stream_context_create([
            'http' => [
                'method' => 'POST',
                'header' => 'Content-Type: application/json; charset=utf-8' . "\r\n" .
                "Authorization: Zoho-oauthtoken " . $this->access_token,
                'content' => $data
            ]
        ]));
        var_dump($http_response_header);
        var_dump($data);
        var_dump($result);
        return $result;
    }

这是var_dump()s 的输出:

<b>Warning</b>:  file_get_contents(https://www.zohoapis.com/crm/v2/coql): failed to open stream: HTTP request failed! HTTP/1.1 401 
 in <b>/home2/.../src/coql.php</b> on line <b>54</b><br />
array(15) {
  [0]=>
  string(13) "HTTP/1.1 401 "
  [1]=>
  string(11) "Server: ZGS"
  [2]=>
  string(35) "Date: Wed, 31 Jul 2019 01:25:28 GMT"
  [3]=>
  string(44) "Content-Type: application/json;charset=utf-8"
  [4]=>
  string(18) "Content-Length: 87"
  [5]=>
  string(17) "Connection: close"
  [6]=>
  string(63) "Set-Cookie: ...; Path=/"
  [7]=>
  string(31) "X-Content-Type-Options: nosniff"
  [8]=>
  string(31) "X-XSS-Protection: 1; mode=block"
  [9]=>
  string(83) "Set-Cookie: crmcsr=...;path=/;Secure;priority=high"
  [10]=>
  string(16) "Pragma: no-cache"
  [11]=>
  string(23) "Cache-Control: no-cache"
  [12]=>
  string(38) "Expires: Thu, 01 Jan 1970 00:00:00 GMT"
  [13]=>
  string(71) "Set-Cookie: JSESSIONID=...; Path=/; Secure"
  [14]=>
  string(26) "X-Download-Options: noopen"
}
string(262) "{"select_query":"select Last_Name, First_Name, Mailing_Street, Mailing_City, Mailing_State, Mailing_Zip, Latitude, Longitude\n      from Contacts\n      where (Latitude between 29.831755 and 29.889647)\n      and (Longitude between -114.994548 and -115.069968)"}"
bool(false)

我该如何解决这个问题?

标签: phpfile-get-contentszoho

解决方案


您的问题是您将方法和内容放在标题中。这是不正确的。它需要在数组中并移到表头之外,例如:

$sResponse = @ file_get_contents($sURL,FALSE,stream_context_create(array('http'=>array(
    'ignore_errors' => TRUE, // critical if you want to see errors in response instead of empty on error
    'method' => 'POST',
    'header' => array(
        'Content-Type: application/json',
        "Authorization: Zoho-oauthtoken $sAccessToken",
        'cache-control: no-cache'
    ),
    'content' => $sJSON
))));

推荐阅读