0

Please help me with this. I am using a webservice and I want the json data as response.

I have following code in my php file :

$cURL = curl_init();
$url='http://localhost:8080/axelor-app/ws/rest/resourcepath/2';
$data=array('data'=>'{
    "offset": 0,
    "limit": 20,
    "data": {
        "_domain": "self.product LIKE :product",
        "_domainContext": { "product": 1}
    }
}');

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL,CURLOPT_POSTFIELDS ,$data);
curl_setopt($cURL,CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-type: application/json',
    'Accept: application/json'
));

$result1 = curl_exec($cURL);

print_r($result1);
print_r(curl_getinfo($cURL));

curl_close($cURL);

And I get output as :

Array
(
    [url] => `http://localhost:8080/axelor-app/ws/rest/resourcepath/2`
    [content_type] => 
    [http_code] => 302
    [header_size] => 270
    [request_size] => 257
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.001812
    [namelookup_time] => 7.0E-5
    [connect_time] => 0.000118
    [pretransfer_time] => 0.00012
    [size_upload] => 287
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 158388
    [download_content_length] => 0
    [upload_content_length] => 287
    [starttransfer_time] => 0.000916
    [redirect_time] => 0
    [redirect_url] => `http://localhost:8080/axelor-app/login.jsp`
    [primary_ip] => 127.0.0.1
    [certinfo] => Array
        (
        )

    [primary_port] => 8080
    [local_ip] => 127.0.0.1
    [local_port] => 57616

I get 302 code and redirected to login page. How to get my json data from this request?

I am stuck from last 4 days. Please help me. Is there any way to make multiple requests using same context? Is there any thing similar like HttpContext(in java) for PHP?

Darshan
  • 1
  • 1
  • 1
    What kind of authorisation is the service expecting? – DarkBee Mar 26 '14 at 11:46
  • U need to specify user and pswd then, see link : http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl – DarkBee Mar 26 '14 at 11:50
  • Server is not using Basic Authentication. Sorry for wrong comment. I just found out this thing by consulting my server administrator. – Darshan Mar 26 '14 at 12:20

3 Answers3

1

Depending on the authentication policy of the server there must be a way you should pass your user name and password , or a temporary authentication token in you request.

This way the server will login and process your request as you like.

Check the server docs for how to optain security token or how to pass your user name and pass in the request

Zalaboza
  • 8,899
  • 16
  • 77
  • 142
1

Inside your curl_setopt($cURL, CURLOPT_HTTPHEADER option add the following one as well:

'Authorization: Basic '. base64_encode("user:password")

Here user and password is the credential that you need to do authentication.

You can also use CURLOPT_USERPWD option for this alternately.

curl_setopt($cURL, CURLOPT_USERPWD, "user:password");
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
0

That's really not how you should structure your array if you are using Json...

You can do:

$data = json_decode('
    data: {
      "offset": 0,
      "limit": 20,
      "data": {
        "_domain": "self.product LIKE :product",
        "_domainContext": { "product": 1}
      }
    }');

then you have $data as type array.

Or you can have something like:

$data = [
  "data" => [
    "offset" => 0,
    "limit" => 20,
    "data" => [
      "_domain" => "self.product LIKE :product",
      "_domainContext": [
        "product": 1
      ]
    ]
  ]
];

then encode the array to json using json_encode()

ddavison
  • 28,221
  • 15
  • 85
  • 110