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. When I pass my SQL statement, file_get_contents()
fails to open the stream.
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' . "
" .
"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:
<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
from Contacts
where (Latitude between 29.831755 and 29.889647)
and (Longitude between -114.994548 and -115.069968)"}"
bool(false)
How do I solve the problem?