- Home /
Question by
Kondrator · Feb 05, 2018 at 09:07 PM ·
webrequestphppost
UnityWebRequest POST to PHP not work
I use documentation with WWWForm: https://docs.unity3d.com/Manual/UnityWebRequest-SendingForm.html
C#:
void Start() {
StartCoroutine(Upload());
}
IEnumerator Upload(){
yield return Upload1();
yield return Upload2();
}
IEnumerator Upload1() {
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add( new MultipartFormDataSection("field1=foo&field2=bar") );
//formData.Add( new MultipartFormFileSection("my file data", "myfile.txt") );
UnityWebRequest www = UnityWebRequest.Post(url, formData);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
Debug.Log( "MULTIPART: " + www.downloadHandler.text );
}
}
IEnumerator Upload2() {
WWWForm form = new WWWForm();
form.AddField("myField", "myData");
UnityWebRequest www = UnityWebRequest.Post(url, form);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
Debug.Log( "WWWForm: " + www.downloadHandler.text );
}
}
PHP:
<?php
echo "POST: ";
print_r( $_POST );
var_dump( $_POST );
echo "GET: ";
print_r( $_GET );
var_dump( $_GET );
?>
Response (MULTIPART and WWWForm):
POST: Array
(
)
array(0) {
}
GET: Array
(
)
array(0) {
}
$_POST is empty... (that's my problem)
Use Unity 2017.3.
I used UnityWebRequest.Send (Unity 5.6) before and it worked for me until it became obsolete.
Thanks.
Comment
Best Answer
Answer by Kondrator · Feb 17, 2018 at 09:16 PM
Solved: https://stackoverflow.com/questions/48627680/unitywebrequest-post-to-php-not-work#new-answer
Your answer
Follow this Question
Related Questions
How to post images to mysql using unity webrequest? 0 Answers
How to get UnityWebRequest data early when using php flush() 1 Answer
How do I use WebRequest() to post data using Javascript in Unity? 0 Answers
Download multiple files from server 2 Answers
Is there a maximimun number of fields for WWWForm.AddField? 1 Answer