Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

http - GET Request from PHP using file_get_contents with parameters

I want to send a GET request to an external site, but also want to send some parameters

for example i've to send a get request to example.com

i want to execute www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz

My code is :

$getdata = http_build_query(
array(
    'uid' => '1',
    'pwd' => '2',
 'msg'=>'3',
 'phone'=>'9999',
 'provider'=>'xyz'
 )
);

$opts = array('http' =>
 array(
    'method'  => 'GET',
    'content' => $getdata
)
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/send.php', false, $context);

I get a server error .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The content option is used with POST and PUT requests. For GET you can just append it as a query string:

file_get_contents('http://example.com/send.php?'.$getdata, false, $context);

Furthermore, the method defaults to GET so you don't even need to set options, nor create a stream context. So, for this particular situation, you could simply call file_get_contents with the first parameter if you wish.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...