Send querystring in a PHP request

Asked

Viewed 98 times

0

I need to access a page that has a predetermined query, but I don’t want the user to be redirected to page, just send the request.

  • To send requests you can use the curl or the file_get_contents() and catch the result.

  • @rray as an example ? type want to request google.com/q=1

  • Look in the documentation, there are clear examples. I’m sorry if I seemed arrogant, but this will help you in the future.

  • is a webservice or a common page?

  • See the example 4 is a good start, if you need anything else let me know.

  • thanks I will test, @rray write a reply please

  • Why did you leave me in a vacuum? It’s webservice or regular page?

  • What you want is to pass a query in querystring http? If that’s the answer see.

  • The idea is an Arduino system, progamei to get command in the url (e.g. Ip/virardireita) however the Arduino and poor in layout, would mount on another server and only send command.

  • it is unclear whether you intend to do this on the server or client side.. If it is from the client side, an ajax request solves, if it is by the server, it would have to use php resources like Curl, fopen, etc. But in the comment you talk about Rduino, which already changes the directions a lot.. Specify the question further.

  • The idea is only require the page, because it is the one that turns on and off , would mount a page in another server q access and exec the function ...

  • @Augustofurlan by his example google.com/q=1 I think my answer solves your problem?

  • @Guilhermenascimento I will test ...

Show 8 more comments

2 answers

0

  • CURL

    The curl requires on some servers have enabled, however you can try enabling by php.ini, remove the ; of ;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/?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/?q=' . $query, false, $context);
    
    echo $resposta;
    
  • fsockopen:

    If you’re going to download a large file maybe file_get_contents and curl have memory dump problems, so follow an example with fsockopen that helps resolve this:

    $query = urlencode('olá mundo');
    
    $fp = fsockopen("www.google.com", 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\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);
    }
    

-2

Dude, why don’t 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>

Browser other questions tagged

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