0

I configured a Node.js server which pulls data from a PostgreSQL database and returns the response in the form of JSON.

The URL is:

http://139.59.232.218:3000/odoo-client?client_req={"opts" : {"login" : "admin","password" : "admin","db" : "cctv_prod_db","host": "139.59.232.218","port": "80"},"moduleDetail" : {"model" : "product.category","method" : "search","args" :[],"filter" : ["sub_category_type","ilike","service"],"fields" : "","domain" : "","offset" : "","limit" : "","sort" : "","resource_id":""}}

While it successfully returns the response when I load it in a web browser, when I try to consume it in PHP no response is returned.

This is my php:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
brandonscript
  • 68,675
  • 32
  • 163
  • 220
user32876
  • 65
  • 2
  • 12

1 Answers1

2

The problem is that the JSON string as a query parameter isn't a valid url string. You'll need to url-encode it:

<?php

$url = 'http://139.59.232.218:3000/odoo-client?client_req='.urlencode('{"opts" : {"login" : "admin","password" : "admin","db" : "cctv_prod_db","host": "139.59.232.218","port": "80"},"moduleDetail" : {"model" : "product.category","method" : "search","args" :[],"filter" : ["sub_category_type","ilike","service"],"fields" : "","domain" : "","offset" : "","limit" : "","sort" : "","resource_id":""}}');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

?>

That said, since you're sending data to the server, you'd be far better off (and it would be more RESTful) to set this up as a POST request instead of a GET.

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220