sending data to external page and retrieving the results

Hello
i am trying to send some data with POST to a page not on my server and get the respond then save the respond to a variable, is it possible ?
i wish to use AJAX for that and also to make the event run from the OK button please.
what i am doing is i got an SMS service that need variables to be send like the account user name and the password and the message and after that i want to get the response from they’re page and show it to the user to inform him that the message was sent ok

Thank you for the help

It is possible, but very difficult and advanced Javascript is required. Normally AJAX is sandboxed to requesting information from the server that the page was loaded from.

Also… remember that everything that is in your web page can be pretty easily viewed by the user. So your user name and password would be pretty easy to get if you put them somewhere in the web page or javascript. PHP has Curl, which will handle this type of request and keep the login information on your server. It will still be complicated, but you can use AJAX to make a request to a blank application which handles the SMS stuff and then returns the information.

Nick

We send SMS using file_get_contents after forming the url with username/password, msg etc

thanks

I would opt for a provider using a rest or soap protocol. We use phpEd to do our remote services (nusoap) and then integrate it into a blank application. It’s more easy to test if you develop this separately. Using sms services is a task that should be done on the server, not on the client. You don’t have any control of that and every message costs money. Similar things if you talk about integration of payment gateways etc.

Thanks guys
so it seams not that straight forward operation
i don’t get it why its so hard to send the data to a remote url ? the event should trigger when there is an insert to the database and of course its not going to be javascript since there is a sensitive data.
the sms service provider can send the response in JASON or XML or STRING so i guess getting the result is simpler using the string format.
we all agree that using a blank application is the way to go, first i know how to send the data to this blank application but how do i get the result to the form who send the data in the first place ? or should i use another application that only receive the information from the blank application ?
i wish there was a macro for this sort of things, it will make my life a lot easier
to clarify things
1- i am getting the sms info from the database (user / pass / sms)
2- i am sending the sms on the insert event
3- the url is like this “http://www.i-message.net/api/sendsms.php?username=@user&password=@pass&message=@message&num
bers=@mobile&sender=@sender&unicode=U&return=full”
4- get the response from the server (STRING) and display it to the user (Admin)

It depends on the provider. Sometimes (with payment gateways) you just send the message and quit. THe provider will call a predefined url of yourself. Then it is possible to use rest (if there’s an interface). Something like:


How to make REST calls in PHP
Example of calling GET request

//next example will recieve all messages for specific conversation
$service_url = 'http://example.com/api/conversations/[CONV_CODE]/messages&apikey=[API_KEY]';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);


Example of calling POST request

//next example will insert new conversation
$service_url = 'http://example.com/api/conversations';
$curl = curl_init($service_url);
$curl_post_data = array(
        'message' => 'test message',
        'useridentifier' => 'agent@example.com',
        'department' => 'departmentId001',
        'subject' => 'My first conversation',
        'recipient' => 'recipient@example.com',
        'apikey' => 'key001'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);


Example of calling PUT request

//next eample will change status of specific conversation to resolve
$service_url = 'http://example.com/api/conversations/cid123/status';
$ch = curl_init($service_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$data = array("status" => 'R');
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);


Example of calling DELETE request

$service_url = 'http://example.com/api/conversations/[CONVERSATION_ID]';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$curl_post_data = array(
        'note' => 'this is spam!',
        'useridentifier' => 'agent@example.com',
        'apikey' => 'key001'
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$response = curl_exec($ch);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);

This is some code I found on the internet. OTher option is to send the full message by using a tcp stack. Again from internet:


<?php
$str3 = "Test Data";
echo $str3;
$fp = stream_socket_client("tcp://192.168.1.26:12000", $errno, $errstr, 30);
if (!$fp) 
{
    echo "$errstr ($errno)<br />
";
    echo "Some problem! </br>";
} 

else 
{   
    fwrite($fp,$str3);
    $str = "";

    while ($str == "")
    {
       $str = fgets($fp, 1024);
    }

    fclose($fp);
    echo $str;
}
?> 

Receive the data:


while (($buffer = fgets($fp, 4096)) !== false) {
    echo $buffer;
}
if (!feof($fp)) {
    echo "Error: unexpected fgets() fail
";
}
fclose($fp);

Thanks a lot Albert
to be honest i didn’t know that file_get_content() can use $url not only local files.
from your experience what is faster curl or file_get_content() ?
also another question
if i put this in a blank app can i send the variables to it and get the result with AJAX from another control app ?

To my knowledge there’s not much difference between a control and a blank application except for the database stuff. Actually I always use a control. As php is a interpreted language the time difference between curl/file operations (which are done in the c++ library) is not significant. Most of the time gets lost by the relatively slow php interpretation. Personally I mostly use curl, so actually I can’t answer this question accurately. Speed issues are mostly on inefficient sql, poor performances of (remote) hosts etc. Not on the php side itself.

Hi
Can you help me to retreive information from a SOAP server that is already running and use that info in a SriptCase Form i have 4 options but i really dont now how to retreive the fields to use them on the form

SOAP 1.1 (Option 1)
A continuaci?n se muestra un ejemplo de solicitud y respuesta para SOAP 1.1. Es necesario reemplazar los marcadores de posici?n que aparecen con valores reales.

POST /AcceDer/wsvigencia.asmx HTTP/1.1
Host: 11.107.8.12
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/EsVigente"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <EsVigente xmlns="http://tempuri.org/">
      <osNSS>string</osNSS>
      <osCURP>string</osCURP>
      <osAgrAfil>string</osAgrAfil>
      <osCPID>string</osCPID>
    </EsVigente>
  </soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <EsVigenteResponse xmlns="http://tempuri.org/">
      <EsVigenteResult>xml</EsVigenteResult>
    </EsVigenteResponse>
  </soap:Body>
</soap:Envelope>

SOAP 1.2 (Option 2)
A continuaci?n se muestra un ejemplo de solicitud y respuesta para SOAP 1.2. Es necesario reemplazar los marcadores de posici?n que aparecen con valores reales.

POST /AcceDer/wsvigencia.asmx HTTP/1.1
Host: 11.107.8.12
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <EsVigente xmlns="http://tempuri.org/">
      <osNSS>string</osNSS>
      <osCURP>string</osCURP>
      <osAgrAfil>string</osAgrAfil>
      <osCPID>string</osCPID>
    </EsVigente>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <EsVigenteResponse xmlns="http://tempuri.org/">
      <EsVigenteResult>xml</EsVigenteResult>
    </EsVigenteResponse>
  </soap12:Body>
</soap12:Envelope>

HTTP GET (Option 3)

A continuaci?n se muestra un ejemplo de solicitud y respuesta para HTTP GET. Es necesario reemplazar los marcadores de posici?n que aparecen con valores reales.

GET /AcceDer/wsvigencia.asmx/EsVigente?osNSS=string&osCURP=string&osAgrAfil=string&osCPID=string HTTP/1.1
Host: 11.107.8.12

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0"?>
xml

HTTP POST (Option 4)

A continuaci?n se muestra un ejemplo de solicitud y respuesta para HTTP POST. Es necesario reemplazar los marcadores de posici?n que aparecen con valores reales.

POST /AcceDer/wsvigencia.asmx/EsVigente HTTP/1.1
Host: 11.107.8.12
Content-Type: application/x-www-form-urlencoded
Content-Length: length

osNSS=string&osCURP=string&osAgrAfil=string&osCPID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0"?>
xml

And this is what i get on the browser when i test the code using HTTP POST

<?xml version="1.0" encoding="UTF-8"?>

-<NewDataSet>


-<Table>

<NSS>3303854734</NSS>

<CURPIMSS>GOFN850801WWCBXSB1</CURPIMSS>

<CURP>000000000000000000</CURP>

<PATERNO>GOMEZ</PATERNO>

<MATERNO>FIERRO</MATERNO>

<NOMBRE>NORMA ANGELICA</NOMBRE>

<AGREGADO_MEDICO>1F1985OR</AGREGADO_MEDICO>

<AGREGADO_AFILIACION>01219856</AGREGADO_AFILIACION>

<FECHA_NACIMIENTO>1985-08-10T00:00:00-06:00</FECHA_NACIMIENTO>

<CONSULTORIO>28</CONSULTORIO>

<TURNO>V</TURNO>

<DIRECCION>16 NO 4404</DIRECCION>

<TELEFONO>410-12-03</TELEFONO>

<COLONIA>BELLAVISTA AMPLIACION 31416</COLONIA>

<VIGENCIA>VIGENTE</VIGENCIA>

<VIGENTE_HASTA>1900-01-01T00:00:00-07:00</VIGENTE_HASTA>

<DH_UMF>NO ASIGNADA</DH_UMF>

<DH_DELEG>99</DH_DELEG>

<DH_CVE_PRESUP>9999999999999999</DH_CVE_PRESUP>

<DH_IP_SERVER>254.254.254.254</DH_IP_SERVER>

<CPID> 2014 -27-B131103X-000000000000000000-1</CPID>

</Table>


-<Table>

<NSS>3303854734</NSS>

<CURPIMSS>BAFF841223WWCBXSB2</CURPIMSS>

<CURP>000000000000000000</CURP>

<PATERNO>BACA</PATERNO>

<MATERNO>FLORES</MATERNO>

<NOMBRE>FRANCISCO ALBERTO</NOMBRE>

<AGREGADO_MEDICO>2M1984OR</AGREGADO_MEDICO>

<AGREGADO_AFILIACION>02119840</AGREGADO_AFILIACION>

<FECHA_NACIMIENTO>1984-12-23T00:00:00-07:00</FECHA_NACIMIENTO>

<CONSULTORIO>28</CONSULTORIO>

<TURNO>V</TURNO>

<DIRECCION>16 NO 4404</DIRECCION>

<TELEFONO>410-12-03</TELEFONO>

<COLONIA>BELLAVISTA AMPLIACION 31416</COLONIA>

<VIGENCIA>VIGENTE</VIGENCIA>

<VIGENTE_HASTA>1900-01-01T00:00:00-07:00</VIGENTE_HASTA>

<DH_UMF>NO ASIGNADA</DH_UMF>

<DH_DELEG>99</DH_DELEG>

<DH_CVE_PRESUP>9999999999999999</DH_CVE_PRESUP>

<DH_IP_SERVER>254.254.254.254</DH_IP_SERVER>

<CPID> 2014 -27-B131103X-000000000000000000-1</CPID>

</Table>

</NewDataSet>

Just use the PHP SoapClient (check the php docs for the details).
http://www.php.net/manual/en/class.soapclient.php