Return data from a URL with Query String

0

There is a service that needs to send data via query string and it gives me a JSON return, however, I'm not getting this return, it does not send the query string.

$url = 'https://www.servico.com.br?nome=abc&cpf=abc&cep=abc'

Having the variable $url I tried the following ways:

$dataReturn = file_get_contents($url);
//Retorna uma mensagem de erro dizendo que os parâmetros não foram passados.
//Como se tivesse enviado apenas: www.servico.com.br

$dataReturn = readfile($url);
//Retorna uma mensagem de erro dizendo que os parâmetros não foram passados.
//Como se tivesse enviado apenas: www.servico.com.br

$dataReturn = new SoapClient($url); // Essa foi no desespero
//Retorna uma mensagem de erro dizendo que não é um wsdl

$dataReturm = Response::json($url);
//Também dá erro. :(

Any suggestions for returning the data?

    
asked by anonymous 04.02.2016 / 18:46

3 answers

2

Try to use file_get_contents () with HTTP Context Options

$getdata = http_build_query(
  [
    'nome' => 'abc',
    'cpf' => 'abc'
  ]
);

$opts = array('https' =>
    array(
        'method'  => 'GET',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $getdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('https://www.servico.com.br', false, $context);
    
04.02.2016 / 18:58
2

Well, technically the "file_get_contents" function already includes sending urls with embedded query strings, so what I can think of is that the error is on your server or more likely on the receiving server, such as "user_agent" blocking of IP, blocking of headers among others.

Test Your Server
Well to test your server you can create 2 simple files, one that receives and another that sends, and display this data on the screen, this may indicate that your server is working correctly ... like the example below:

[Submit]

<?php
var_dump(file_get_contents("http://seuserver.com.br/recebe.php?nome=abc&cpf=abc&cep=abc"));
?>

[Receives]

<?php
print_r($_GET);
?>

If you can see the data you have sent, then it falls into the second case, that the receiving server has some restriction ... the most common is user_agent or badly formatted header, so you can simulate an agent, because many servers make the block to just not be accessed by robots, so using the answers of colleagues, it would look something like this:

[Submit]

<?php
 $opts = array(
  "http" => array(
   "method" => "GET",
   "user_agent" => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"
 )
);
$context  = stream_context_create($opts);
$response = file_get_contents("http://seuserver.com.br/recebe.php?nome=abc&cpf=abc&cep=abc", false, $context);
var_dump($response);
?>

[Receives]

<?php
 print_r($_GET);
 echo "\r\n";
 echo $_SERVER["HTTP_USER_AGENT"];
?>

Finally, if this solution does not resolve, I advise you to start with something more elaborate, such as using the cURL or make your own HTTP connection module using fsockopen .

    
04.02.2016 / 22:25
0

Try CURL, use this code:

$url = 'https://www.servico.com.br'

$querydata = array(
    'nome' => 'abc',
    'cpf' => 'abc',
    'cep' => 'abc'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($querydata));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$jsonResult = curl_exec($ch);
curl_close($ch);

echo $jsonResult;
    
05.02.2016 / 16:47