How to make an HTTP request in PHP?

后端 未结 3 1992
不知归路
不知归路 2020-12-21 12:09

I have to send an SMS by making an HTTP request via GET method. The link contains information in the form of GET variables, e.g.

http://www.somelink.com/file         


        
相关标签:
3条回答
  • 2020-12-21 12:29

    Lets assume that we want to retrive http://www.google.com

    $cURL = curl_init();
    $setopt_array = array(CURLOPT_URL => "http://www.google.com",    CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array()); 
    curl_setopt_array($cURL, $setopt_array);
    $json_response_data = curl_exec($cURL);
    print_r($json_response_data);
    curl_close($cURL);
    

    /* cURL is preinstalled by goDaddy.com and many other php hosting providers it is also preinstalled in wamp and xampp good luck. */

    0 讨论(0)
  • 2020-12-21 12:33

    I think the easiest way to make an HTTP request via GET method from PHP is using file_get_contents().

    <?php
    $response = file_get_contents('http://example.com/send-sms?from=12345&to=67890&message=hello%20there');
    echo $response;
    

    Don’t forget to see the notes section for info on PHP configuration required for this to work. You need to set allow_url_fopen to true in your php.ini.

    0 讨论(0)
  • 2020-12-21 12:54

    The easiest way is probably to use cURL. See http://codular.com/curl-with-php for some examples.

    0 讨论(0)
提交回复
热议问题