简体   繁体   中英

Execute external Get url request

So, I have this method where I need to call an external url (different domain). It's something like http://192.168.2.2:9090/send?num=100&txt=text; Is there any way to do this without using curl?

I guess I should clarify that I have tried using curl with the yii-curl extension but somehow it doesn;t seem to work. It works when I supply a whole formatted url, but if I try to modify the url with params for num and txt, it doesn't work for some reason. Preferably I am looking for a solution without curl, but if that is not possible I could use some help with how to format and execute a proper url so I can also supply params to the url. Thanks.

Edit: I don't think file_get_contents() will work as the url is actually to an SMS gateway that sends sms. the phone number and sms text is supplied as params. Let me know if I am guessing it wrong.

Edit 2 : This is what I tried after the suggestions here.

public function sendTXTSMS($sentToNum,$text)
        {

            $construct_url="http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";
            file_get_contents($construct_url);
        }

And then calling it like,

    $text='Lorem ipsum dolor ........ ';
$this->sendTXTSMS(XXXXXXXXXX,$text)

XXXXXXXXXX is of course the phone number masked here.

Now I am getting an HTTP request failed! HTTP/1.1 400 Bad Request HTTP request failed! HTTP/1.1 400 Bad Request error. allow_url_fopen is enabled and I can access the url fine by typing it on a browser. Also tried using urlencode on the url.

If it's a GET request you can use file_get_contents($url); .

If you need more options you can try the HTTP library , but there's little reason to not directly use libcurl. It's standard practice.

The fact it's connecting to a service related to SMS is irrelevant. If it's a URL for a web service on a server you can connect to, you can make a request to it.

file_get_contents() will work if allow_url_fopen is enabled in php.ini , but I think your problem is this:

It works when I supply a whole formatted url, but if I try to modify the url with params for num and txt , it doesn't work for some reason.

You need to encode the data:

$test = urlencode($text);
$sentToNum = urlencode($sentToNum);
$construct_url = "http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";

是的,您可以使用file_get_contents http://php.net/manual/en/function.file-get-contents.php

Using curl should be a better option since you can easily deal with http status code...

But with or without curl, you need to build your url correctly, urlencode should be used on params, not on url :

$sentToNum = urlencode($sentToNum);
$text = urlencode($text);
$construct_url="http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM