469,343 Members | 5,478 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

webservices in php

Ciary
247 Expert 100+
good morning/afternoon/evening/night fellow coders,

like a few times before i'm stuck with a problem. this time it hes everything to do with webservices or WCF.
in the end, i want to convert an xml/access/SQL database (depends on what the user uploads) to a simple multi-dimentional array.
but it isn't what i was trying to do now since i'm rather new to WCF. so, i started with a simple 'hello world'

coding the webservice wasn't difficult, but now i tried to use it in php using the next code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header('Content-Type: text/plain');
  3.  
  4. echo "WCF Test\n";
  5.  
  6. $client = new SoapClient('webservice try 1/webservice try 1/Service1.svc?wsdl');
  7.  
  8. $retval = $client->Test();
  9.  
  10. echo $retval;
  11. ?>
like you could have guessed. this didnt work. i got the next error:

-------------------------------------------------------------------------------------------------------------------
WCF Test
<br />
<b>Warning</b>: SoapClient::SoapClient() [<a href='soapclient.soapclient'>soapclient.soapclient </a>]: I/O warning : failed to load external entity &quot;webservice try 1/webservice try 1/Service1.svc?wsdl&quot; in <b>C:\xampp\htdocs\page\testWCF.php</b> on line <b>6</b><br />
<br />
<b>Fatal error</b>: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'webservice try 1/webservice try 1/Service1.svc?wsdl' : failed to load external entity &quot;webservice try 1/webservice try 1/Service1.svc?wsdl&quot;
in C:\xampp\htdocs\page\testWCF.php:6
Stack trace:
#0 C:\xampp\htdocs\page\testWCF.php(6): SoapClient-&gt;SoapClient('webservice try ...')
#1 {main}
thrown in <b>C:\xampp\htdocs\page\testWCF.php</b> on line <b>6</b><br />
-------------------------------------------------------------------------------------------------------------------

can anyone tell me how this should be done properly?
Apr 15 '09 #1
18 9703
Ciary
247 Expert 100+
problem solved. my url was wrong.

but now i have a new problem. 'Unsupported Media Type' on line 8

does anyone know how i can fix this?
Apr 15 '09 #2
Dormilich
8,651 Expert Mod 8TB
shouldn't be the media type of a SOAP request be "text/xml" or "application/soap+xml"?
Apr 15 '09 #3
Ciary
247 Expert 100+
if you mean that i need to change my header information, i tried that and it didn't work. also, i can connect to my web service since i can var_dump my functions:
Expand|Select|Wrap|Line Numbers
  1. var_dump($client->__getFunctions());
result:
-------------------------------------------------------------------------------------------------------------------
array(3) {
[0]=>
string(44) "GetDataResponse GetData(GetData $parameters)"
[1]=>
string(95) "GetDataUsingDataContractResponse GetDataUsingDataContract(GetDataUsingDataContract $parameters)"
[2]=>
string(35) "TestResponse Test(Test $parameters)"
}
-------------------------------------------------------------------------------------------------------------------

but asking it to execute a function still doesn't work. even if it doesn't need parameters.

i don't know what you or the error mean by 'media type'. and i dont know how i can change it either.
Apr 15 '09 #4
Dormilich
8,651 Expert Mod 8TB
according to the manual, you call a soap function vial SoapClient::__soapCall()
Apr 15 '09 #5
Ciary
247 Expert 100+
i know, but one of the examples on the bottom of the page says otherwise.

anyway, here is the code i tried (with __call) but it still gives the same error.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header('Content-Type: text/plain');
  3.  
  4. echo "WCF Test\n";
  5.  
  6. $client = new SoapClient('http://localhost:2024/Service1.svc?wsdl');
  7.  
  8. var_dump($client->__getFunctions());
  9.  
  10. $empty = array();
  11.  
  12. $webService = $client->__soapCall('Test', $empty);
  13. ?>
i also tried __call but it doesnt work either.
Apr 15 '09 #6
Dormilich
8,651 Expert Mod 8TB
where exactly does the function fail? is there any specific reasons given in the error message?
Apr 15 '09 #7
Ciary
247 Expert 100+
this is the full error message. i cant tell you more then this since i dont know it either. the functions i'm using are standard functions provided by visual studio 2008. i wrote the test function myself but i tried everything with both test and getData.

error message:
-------------------------------------------------------------
<br />
<b>Fatal error</b>: Uncaught SoapFault exception: [HTTP] Unsupported Media Type in C:\xampp\htdocs\page\testWCF.php:12
Stack trace:
#0 [internal function]: SoapClient-&gt;__doRequest('&lt;?xml version=&quot;...', 'http://localhos...', 'http://tempuri....', 1, 0)
#1 C:\xampp\htdocs\page\testWCF.php(12): SoapClient-&gt;__soapCall('Test', Array)
#2 {main}
thrown in <b>C:\xampp\htdocs\page\testWCF.php</b> on line <b>12</b><br />
---------------------------------------------------------------
Apr 15 '09 #8
Dormilich
8,651 Expert Mod 8TB
I can only guess, but it seems like the soap message transfer (HTTP) uses an inappropriate media type, there should be a documentation somewhere which states what media types are accepted by the soap recipient.

maybe the SoapVar class can help you. also read the comments in PHP's SOAP manual, maybe you find useful information there.
Apr 15 '09 #9
Ciary
247 Expert 100+
a new day and still stuck with the same problem.

like you already said. by media type they mean "application/soap + xml" but i've no idea how i could change this. the soapvar class has nothing to do with this. its just a better way to send data along with my soapcall.

can you tell me how i can change my mediatype?

edit: the next info could be more helpful:
when i used this code(see codeblock1) the connection simply times out instead of causing an error. when i tried it with 1(see codeblock2) it gave the same error as before.

cb1:
Expand|Select|Wrap|Line Numbers
  1. $wsdl_url =  "http://localhost:2024/Service1.svc?wsdl";
  2. $options = array('soap_version'   => SOAP_1_2);            //added this!!
  3. $client     = new SoapClient($wsdl_url, $options);
  4.  
  5. $params=array('value'=>'1');
  6.  
  7. try{
  8.     print_r($client->Test());
  9. }catch(SoapFault $fault){
  10.         echo ' Error Message : ',
  11.         $fault->getMessage(); 
  12. }
cb2:
Expand|Select|Wrap|Line Numbers
  1. $wsdl_url =  "http://localhost:2024/Service1.svc?wsdl";
  2. $options = array('soap_version'   => SOAP_1_1);            //changed this!!
  3. $client     = new SoapClient($wsdl_url, $options);
  4.  
  5. $params=array('value'=>'1');
  6.  
  7. try{
  8.     print_r($client->Test());
  9. }catch(SoapFault $fault){
  10.         echo ' Error Message : ',
  11.         $fault->getMessage(); 
  12. }
Apr 16 '09 #10
Ciary
247 Expert 100+
ok, i got my problem solved. it wasnt a problem of php but a problem in my webservice.

i needed to change my web.config. in it, the binding as standard set to wsHttpBinding, but php5 doesnt support this yet. therefore you need to change it to basicHttpBinding. dont really know whats different but i do know it works.

ty for your help Dormilich. i would have given up without you :)
Apr 16 '09 #11
Dormilich
8,651 Expert Mod 8TB
given that I've never dealt with WSDL before, this is a great achievement (of you). now I know someone whom I can ask if I should try it myself *g*.
Apr 16 '09 #12
Ciary
247 Expert 100+
if you need it, just ask and ill see what i can do to answer :) i've only echoed 'hello world' and a send parameter so far but it looks really simple. all code in the webservice is standard vb.net code so it isn't that difficult either.

now lets see if i can transfer files ^^
Apr 16 '09 #13
Dormilich
8,651 Expert Mod 8TB
I recommend curl, wsdl is not designed for file transfer (well, if you mean pure file transfer).

PS: can't use vb.net, I've got a unix server
Apr 16 '09 #14
Ciary
247 Expert 100+
problem is that it's a file transfer thats going to be used in my webservice. the whole reason for using a webservice is because i want to convert an uploaded access/xml or SQL database to a simple array which i can use in my website.

i'll find a way to get it working :)

for helping you with webservices, building one in php/c/java isnt difficult either you only have a few lines to declare it's a webservice and not a stand-alone program. i'm not sure about c but i know the other 2 are compatible with Unix.if you need programming any of those (apart from php since i think you might be able to) i'll be glad to help.
Apr 16 '09 #15
Dormilich
8,651 Expert Mod 8TB
converting an XML file into an array can be achieved by deserialization

XML ==(XSLT)=> WDDX ==(wddx_deserialize())=> PHP array

(though there are certainly more possibilities)
Apr 16 '09 #16
Ciary
247 Expert 100+
i know, xml isnt the problem. even SQL shouldnt be that hard. it's access thats causing the trouble. dont know how that could be converted using only php.
Apr 16 '09 #17
Dormilich
8,651 Expert Mod 8TB
sorry, can't help there since I don't know anything about access.
Apr 16 '09 #18
Ciary
247 Expert 100+
i just found a way: first make a function in which you create a file using exacly the same one as the uploaded file. then put read the uploaded file in php using 'fopen()', 'fread()' and 'fclose()' and put it in a variable. after that, you just send the string in a second function which it writes back into the file.

another option i just found to dynamically select a database is the use of odbc with a connection string. it makes it possible to connect to an access database without the problem of needing a DSN(data source name) but if i have questions using this, ill post them in another topic since it has nothing to do with webservices.
Apr 17 '09 #19

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Erik P. Vinther | last post: by
8 posts views Thread by Allan Ebdrup | last post: by
1 post views Thread by Diffident | last post: by
5 posts views Thread by cyberstrike | last post: by
2 posts views Thread by Antuane | last post: by
1 post views Thread by Mike | last post: by
8 posts views Thread by Komandur Kannan | last post: by
5 posts views Thread by ChrisM | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.