Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Salanyel · Dec 03, 2015 at 01:08 PM · web services

How to consume a php webservice in Unity ?

Hi,

I'm actually maling a sample to test the web service consumption into Unity. In my localhost I've created a little php webservice : index.php :

     class Donne
     {
       function donne($i)
       {
         return $i;
       }
       function hello()
       {
         return "hello world";
       }
 
       function add($a, $b)
       {
           $c = $a + $b;
           return $c;
       }
     }
 
     try
     {
       $server = new SoapServer(null, array('uri' => 'http://127.0.0.1/webservices/index.php'));
 
       $server->setClass("Donne");
       $server->handle();
     }
     catch(Exception $e)
     {
       echo "Exception: " . $e;
     }
 ?>

And I created an other page to use it : test.php

 <?php
 try
 {
   $clientSOAP = new SoapClient( null,
     array (
       'uri' => 'http://127.0.0.1/webservices/index.php',
       'location' => 'http://127.0.0.1/webservices/index.php',
       'trace' => 1,
       'exceptions' => 0
   ));
 
   $ret = $clientSOAP->__call('hello', array());
   echo $ret;
 
   echo '<br />';
 
   if (isset($_GET['a']) && isset($_GET['b']))
   {
       $ret = $clientSOAP->__call('add', array('a'=>$_GET['a'], 'b'=>$_GET['b']));
       echo $ret;
   }
   else
   {
       echo 'no';
   }
 }
 catch(SoapFault $f)
 {
   echo $f;
 }
 ?>

When I acces if from the following URL : (http://127.0.0.1/webservices/test.php?a=5&b=8), I've got the correct answer : Hello World
13

But when I try to access it from the Unity execution :

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class WebService : MonoBehaviour {
 
     public GameObject text;
     public string url = "http://127.0.0.1/webservices/test.php";
 
     private WWWForm wwwForm;
     private Text m_text;
 
     // Use this for initialization
     IEnumerator Start () {
         wwwForm = new WWWForm ();
         wwwForm.AddField ("a", 5);
         wwwForm.AddField ("b", 8);
 
         m_text = text.GetComponent<Text> ();
         WWW www = new WWW(url, wwwForm);
         yield return www;
         m_text.text = www.text;
 
     }
 }

I've got "Hello World
no". I've read that all WWWForms are GET by default but the $_GET in php is not valid.

What did I miss ?

Thx for the help.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Dec 03, 2015 at 02:01 PM 0
Share

is it possible you need to sort of add the headers manually, eg http://answers.unity3d.com/answers/675582/view.html

avatar image Fattie · Dec 03, 2015 at 03:18 PM 0
Share

Just BTW you'd surely just use Parse.com or any of the other baas these days, for "cloud" odds and ends

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Dec 03, 2015 at 03:03 PM

WWWForms is POST by default. Get requests have their parameters / data inside the URL. A GET request can have a body, but it's ignored by the parser since it has no meaning for GET requests. See this.

So either add your parameters to the URL, just like you did in your browser test, or change the serverside code to accept POST requests and use the parsed body data instead.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Dec 03, 2015 at 03:18 PM 0
Share

it's a good idea to just stick with POST change the serverside

avatar image Salanyel · Dec 03, 2015 at 04:03 PM 0
Share

All hail to the post method ! That was it.

I check again the Unity doc and I've been lure by the following line : "The WWW class can be used to send both GET and POST requests to the server. The WWW class will use GET by default and POST if you supply a postData parameter."

Thank you !

avatar image Fattie Salanyel · Dec 03, 2015 at 05:25 PM 0
Share

you should try Parse.com though

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Web Bluetooth and Unity with Webgl,Web Bluetooth and Unity WebGl? 1 Answer

Post Json request Unity web 1 Answer

How to connect unity to MySQL (MariaDB) API, Unity ver 2019.4? 0 Answers

Game port to unity: Web posting 0 Answers

How to consume svc web service in ios 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges