簡體   English   中英

如何修復由 JSON 內容中的負數引起的 php file_get_contents 警告?

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

我正在嘗試使用 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()的輸出:

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

我該如何解決問題?

您的問題是您將方法和內容放在標題中。 那是不正確的。 它需要在數組中並移到標題之外,例如:

$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