Httprequest dont work xD

Just starting out? Need help? Post your questions and find answers here.
skinkairewalker
Posts: 120
Joined: Tue Jun 14, 2016 7:17 pm

Httprequest dont work xD

Post by skinkairewalker »

i am trying get message from PHP application , but dont Works ...

code >

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData)
  Debug Result$
    If Success
      Debug Result$
    Else
      Debug "HTTPRequest(): Error"
    EndIf
  EndProcedure
  
  HTTPRequest(#PB_HTTP_Get, "localhost/test.php", "", @HttpGetEvent()) ;// test.php return HelloWorld


falsam
Posts: 286
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: Httprequest dont work xD

Post by falsam »

Hello skinkairewalker.

You can't run HTTPRequest from the IDE because the internal server does not support PHP.

-Do not use the Debug function
-Use The Compile menu -> Transfer
-Launch The result from your local or remote server.

■ Example with this codes.

-receivedata3.php

Code: Select all

<?php
   $array = array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
   echo json_encode($array);
?>
-httprequest.pb

Code: Select all

Enumeration
  #Json
  #mainForm
  #Debug
EndEnumeration

Declare HttpGetEvent(Success, Result$)

OpenWindow(#mainForm, 0, 0, 0, 0, "Demo HTTPRequest()", #PB_Window_Background)
EditorGadget(#Debug, 0, 0, WindowWidth(#mainForm), WindowHeight(#mainForm))

HTTPRequest(#PB_HTTP_Get, "receivedata3.php", "", @HttpGetEvent())

Procedure HttpGetEvent(Success, Result$)
  Protected i, buffer.s
 
  If Success
    If ParseJSON(#Json, Result$) 
      
      ; transfer the php array to a map
      NewMap Values.l()
      ExtractJSONMap(JSONValue(#Json), Values())
      
      ; display the result
      ForEach Values()
        buffer + MapKey(Values()) + " = " + Values() + #CRLF$
      Next Values()
      SetGadgetText(#Debug, Buffer)
      
    EndIf 
  Else
    SetGadgetText(#Debug,  "HTTPRequest(): Error")
  EndIf
EndProcedure
■ Demo (Url Remove)
Last edited by falsam on Mon Mar 26, 2018 5:46 pm, edited 1 time in total.

➽ Windows 11 - jdk-11.0.2 - SB 3.00 - Android 15
https://falsam.com

Sorry for my poor english
skinkairewalker
Posts: 120
Joined: Tue Jun 14, 2016 7:17 pm

Re: Httprequest dont work xD

Post by skinkairewalker »

thanks falsam by you answer :)

i am trying if CGI Works ... but i have same ...
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Httprequest dont work xD

Post by Peter »

@skinkairewalker:

i guess that this is a 'Access-Control-Allow-Origin' - Problem.

Add the following line to your php-script:

Code: Select all

header('Access-Control-Allow-Origin: http://127.0.0.1:9080');
or the following line to your pb-cgi-code:

Code: Select all

WriteCGIHeader("Access-Control-Allow-Origin", "http://127.0.0.1:9080", #PB_CGI_LastHeader) ; (take a look at #PB_CGI_LastHeader in the PB-Help)
Greetings ... Peter
Post Reply