2

I try to get odata produced by a navision webservice. When I directly access the url given using chrome browser, the page asks for user name and password and then chrome shows xml data as expected.

But when i use a PHP script, it always returns "1".

My code looks like this:

$url = 'http://103.7.1.182:14048/DynamicsNAV71-6/OData/Company(\'Unit%20G%205\')/Item_Master_on_hand_no_Desc';
$login = 'Gem-gae\senzo1:Bsbsenzo2018';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
      'Content-Type:application/x-www-form-urlencoded',                                                                                
      'Content-Length: ' . strlen(1))                                                                       
  );
$output = curl_exec($ch);
curl_close($ch);
echo $output; 

Is there any mistakes with that code ? Thanks for your help

Adrian W
  • 4,563
  • 11
  • 38
  • 52
Budi Utomo
  • 21
  • 3
  • Why `'Content-Length: ' . strlen(1))` ? You may want to read this: https://stackoverflow.com/a/22564260/1911019 – Alberto Aug 29 '18 at 12:49
  • Thanks Alberto, because there's no data post sent needed exept username and password. May you can access that web service url given? thanks again Alberto... – Budi Utomo Aug 29 '18 at 12:58
  • Shouldn’t you use GET instead of POST? – Mak Sim Aug 29 '18 at 18:48

1 Answers1

1

There's no mistake on your code, it successfully connects to the external server via cURL and retrieves the data. The value in $output (if you do a var_dump) is true (that's why you see 1 if you echo it).

Said that, the issue is on the server you are contacting (103.7.1.182:14048). You probably need to send some data or something, I can't tell you exactly what because I don't know what is in that server. To send data, you can add a curl_setopt line:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

Where $data is an array.

Alberto
  • 674
  • 13
  • 25