Send querystring in a request with PHP

0

I need to access a page that has a pre-determined query, but I do not want the user to be redirected to the page, just send the request.

    
asked by anonymous 18.02.2016 / 17:18

2 answers

0
  • CURL

    The curl requires on some servers have enabled, however you can try to enable by php.ini, remove ; from ;extension=... , for example:

    ;Windows server
    extension=php_curl.dll
    
    ;Like-unix server
    extension=curl.so
    

    Example:

    $query = urlencode('Olá mundo');
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.google.com.br/?q=' . $query);
    curl_setopt($ch,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $resposta = curl_exec($ch);
    curl_close($ch);
    
    echo $resposta;
    
  • file_get_contents :

    <?php
    // Create a stream
    $opts = array(
      'http'=>array(
          'method' => "GET",
          'header' => "Accept-language: en\r\n"
       )
    );
    
    $context = stream_context_create($opts);
    
    $query = urlencode('Olá mundo');
    
    $resposta = file_get_contents('http://www.google.com.br/?q=' . $query, false, $context);
    
    echo $resposta;
    
  • fsockopen :

    If you are going to download a large file, you may find that file_get_contents and curl have memory dump problems, so follow an example with fsockopen that helps solve this:

    $query = urlencode('olá mundo');
    
    $fp = fsockopen("www.google.com.br", 80, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out  = "GET /?q=' . $query . ' HTTP/1.1\r\n";
        $out .= "Host: www.google.com.br\r\n";
        $out .= "User-Agent: ' . $_SERVER['HTTP_USER_AGENT'] . '\r\n";
        $out .= "Connection: Close\r\n\r\n";
    
        fwrite($fp, $out);
    
        while (false === feof($fp)) {
            echo fgets($fp, 128);
        }
        fclose($fp);
    }
    
18.02.2016 / 21:16
-2

Dude, why do not you use javascript to do this GET? You can use this here:

<script type="text/javascript">
  $(document).ready(function(){
       var url = "http://url.com";
       $.get(url, function(response){
           // Executar alguma coisa com a resposta do endereço
           console.log(response);
       });
  });
</script>
    
18.02.2016 / 20:37