how to send HTTP request by GET method in PHP to another website

后端 未结 5 2113
情话喂你
情话喂你 2021-02-20 06:57

I\'m developing a web application for sending SMS to mobile from website like 160by2.

I can prepare the URL required for the HTTP GET request as mentioned in the API pro

相关标签:
5条回答
  • 2021-02-20 07:41

    Your problem is the way you are constructing the URL. The spaces you are including in the query string will result in a malformed request URL being sent.

    Here is an example that replicates your circumstances:

    request.php:

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 
        'http://your_server/response.php?foo=yes we can&baz=foo bar'
    );
    $content = curl_exec($ch);
    echo $content;
    

    response.php:

    <?php
    print_r($_GET);
    

    The output of request.php is:

    Array
    (
        [foo] => yes
    )
    

    The reason for this is the query string is not properly encoded and the server interpreting the request assumes the URL ends at the first space, which in this case is in the middle of the query: foo=yes we can&baz=foo bar.

    You need to build your URL using http_build_query, which will take care of urlencoding your query string properly and generally makes the code look a lot more readable:

    echo http_build_query(array(
        'user'=>'abc',
        'password'=>'xyz',
        'msisdn'=>'1234',
        'sid'=>'WebSMS',
        'msg'=>'Test message from SMSLane',
        'fl'=>0
    ));
    

    You also need to set CURLOPT_RETURNTRANSFER:

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    0 讨论(0)
  • 2021-02-20 07:41

    The below code will make a HTTP request with curl and return the response in $content.

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,'http://www.anydomain.com/anyapi.php?a=parameters');
    $content = curl_exec($ch);
    echo $content;
    ?>
    
    0 讨论(0)
  • 2021-02-20 07:44
    <?php
    print file_get_contents("http://some_server/some_file.php?some_args=true");
    ?>
    
    0 讨论(0)
  • 2021-02-20 07:53

    Try using following code.

    $r = new HttpRequest('http://www.xencomsoftware.net/configurator/tracker/ip.php', HttpRequest::METH_GET);
    try {
            $r->send();
             echo $r->getResponseCode(); 
             if ($r->getResponseCode() == 200) 
            {
            }
        }
    

    make sure that you have loaded HttpRequest extension (share object0 in your server.

    0 讨论(0)
  • 2021-02-20 08:00

    nothing wrong with your code that I can see right off. have you tried pasting the url into a browser so you can see the response? you could even use wget to download the response to a file. I suppose if you want to try fsocket that you would use port 80 as that's the default http port.

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