Page 1 of 1

Httprequest dont work xD

Posted: Sat Oct 15, 2016 8:02 pm
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



Re: Httprequest dont work xD

Posted: Sat Oct 15, 2016 10:36 pm
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)

Re: Httprequest dont work xD

Posted: Sat Oct 15, 2016 11:12 pm
by skinkairewalker
thanks falsam by you answer :)

i am trying if CGI Works ... but i have same ...

Re: Httprequest dont work xD

Posted: Mon Oct 17, 2016 7:17 am
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