简体   繁体   English

如何修复由 JSON 内容中的负数引起的 php file_get_contents 警告?

[英]How do I fix a php file_get_contents warning caused by a negative number in JSON content?

I'm trying to use file_get_contents() to perform an SQL query using Zoho's COQL API ( https://www.zoho.com/crm/developer/docs/api/Get-Records-through-COQL-Query.html ) to get a list of customers within a longitude and latitude boundary.我正在尝试使用 file_get_contents() 使用 Zoho 的 COQL API ( https://www.zoho.com/crm/developer/docs/api/Get-Records-through-COQL-Query.html ) 执行 SQL 查询获取经纬度边界内的客户列表。 When I pass my SQL statement, file_get_contents() fails to open the stream.当我传递我的 SQL 语句时, file_get_contents()无法打开流。

I've been troubleshooting this for the better part of two hours now, and from what I can tell, the issue only occurs when a negative number is present in the query.我已经在两个小时的大部分时间里解决了这个问题,据我所知,只有当查询中存在负数时才会出现问题。 If I remove the hyphen, I'm able to perform a successful query and get a response from the server.如果删除连字符,则可以执行成功的查询并从服务器获得响应。 I don't understand what's going on or how to fix it.我不明白发生了什么或如何解决它。

I've changed the longitude and latitude numbers for privacy, but otherwise this is the code I'm using.我已经更改了隐私的经度和纬度数字,但除此之外,这是我正在使用的代码。


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

Here's the output of the var_dump() s:这是var_dump()的输出:

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

How do I solve the problem?我该如何解决问题?

Your problem is that you put the method and content inside the header.您的问题是您将方法和内容放在标题中。 That's incorrect.那是不正确的。 It needs to be in the array and moved outside the header, such as:它需要在数组中并移到标题之外,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM