Reputation: 1
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?
Upvotes: 0
Views: 186
Reputation: 39415
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");
Upvotes: 1
Reputation: 9007
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
Upvotes: 1
Reputation: 29062
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()
Upvotes: 0