How To Use cURL without http://www Prefix

2019-09-05 22:07发布

问题:

i have this problem. I neent to cURL an api link given to me by my bulksms gateway for use in the site i'm building. The link is smsplus4.routesms.com . This link does not work if you access it with the www or http://www prefixes. Now i have the curl function

function curl_get_contents($url)
{   
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$api = "smsplus4.routesms.com";
curl_get_contents($api);

which did not work because of the missing http://www prefix. Pls what do i do? how do i get the cURL to work for me without the http://www prefix? Thanks in advance.

回答1:

Running exactly the code above seems to work for me.

$ cat test.php
<?php

function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$api = "smsplus4.routesms.com";
echo curl_get_contents($api);  <-- Added echo to see the output

?>
$ php test.php | head


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <!--<![endif]-->
    <!-- BEGIN HEAD -->
    <head>
        <meta charset="utf-8"/>
        <title>SmsPlus :: Login </title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">


标签: php http curl web