Load url on same page

Asked

Viewed 559 times

1

I bought the service from an SMS company. But to send the SMS I have to "run" the link below:

https://site.com/apiJSON.php?data={"login":"[email protected]","senha":"SENHA","campanha":"ID 1234","mensagens":{"1":{"numero":"2799999999","msg":"MINHA MENSAGEM","data":"2016-08-11 09:20:00"}}}

Basically a JSON call.

The problem is that I need to send the SMS in the middle of a PHP function. And I can’t call this link to send the SMS.

Someone has an idea?

  • What is the problem you are having to use it?

  • It’s just that I have no idea how to call her "spin" you know. If you paste this link with the correct parameters in the address bar and press enter it sends msg. The problem is that I don’t know how to run the link through PHP.

  • A redirect wouldn’t work for your problem? http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php

4 answers

2

Experiment with Curl:

function do_sms($serv) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($curl, CURLOPT_URL, $serv);
    curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;
}

do_sms('https://site.com/apiJSON.php?data={"login":"[email protected]","senha":"SENHA","campanha":"ID 1234","mensagens":{"1":{"numero":"2799999999","msg":"MINHA MENSAGEM","data":"2016-08-11 09:20:00"}}}');

Or file_get_contents:

file_get_contents('https://site.com/apiJSON.php?data={"login":"[email protected]","senha":"SENHA","campanha":"ID 1234","mensagens":{"1":{"numero":"2799999999","msg":"MINHA MENSAGEM","data":"2016-08-11 09:20:00"}}}');

1


You can use the json_encode to format the json, and curl to place the order:

function enviarSMS($email, $senha, $numero, $msg){
    $curl = curl_init("https://site.com/apiJSON.php?data=");
    $data = date('Y-m-d H:i:s');

    $array = array('login' => $email,
                   'senha' => $senha,
                   'campanha'  => 'ID 1234',
                   'mensagens' => array('1' => array('numero' => $numero,
                                                     'msg' => $msg,
                                                     'data' => $data)));

    $json = json_encode($array);

    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);

    $jsonExec = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);
    $resposta = json_decode($jsonExec, true);

    echo "Status: $status\n";
    echo $resposta;
}

And to use it, do so:

enviarSMS('foo@bar', '123baz', '2799999999', 'mensagem');
  • 1

    Vlw, you solved my problem!

1

First use CURL to connect with another service.

That way, for example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link_para_chamar);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '10');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

// Habilita cache do DNS (remover o // para habilitar):
//curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, true);
//curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, '3600');

// Habilita verificação de SSL (em caso de problema defina ambos para FALSE, não é recomendado desligar!):
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);


$resposta = curl_exec($ch);
$erroSite = curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200 ? true : false;
$erroCurl = curl_error($ch);
curl_close($ch);

So to define the $link_para_chamar, utilize json_encode:

$json = [];
$json['login'] = '[email protected]';
$json['senha'] = 'senha';
$json['campanha'] = 'ID 1234';

$json['mensagens'][1]['numero'] = '2799999999';
$json['mensagens'][1]['msg'] = 'MENSAGEM';
$json['mensagens'][1]['data'] = '2016-08-11 09:20:00';

$link_para_chamar = 'https://site.com/apiJSON.php?data='.json_encode($json);

-1

Just implementing what the above friend stressed.

To be more complete, use javascript. Example.

<form id="send">
  <input type="text" name="login" />
  <input type="text" name="number" />
</form>

And in the javascript file recover the data and pass to Curl in PHP.

$.ajax({
  url: 'arquivo.php',
  data: $(this).seriaze(),
  type: 'POST'
});

And then just recover the data in PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.