开发者

Is it possible to use php to make a request to another server and get its response?

For instance user.php -> make a post request to a server http://www.exampe.com, example.com redirects to user.php and puts some post parameters as well to user.开发者_运维百科php.

I want a function in php to do that? Anyone with a solution for me?

I cannot use ajax for this.


Yes, you can achieve such functionality using cURL from PHP. Check out the examples in the PHP documentation.


For simple requests file_get_contents() will do the trick.

For more complex requirements, you van use cURL, a simple example (returning both headers and resonse body):

<?php
function curlPull($url) {
    $cPtr = curl_init();
    curl_setopt_array($cPtr, array(CURLOPT_URL => $url, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true));
    return curl_exec($cPtr);
}
print_r( curlPull('http://www.example.com') );
?>

cURL allows for many more options to be tweaked, take a look at curl_setopt


I'll speculate that eventually such questions as

  • Is remote access feasible withOUT cURL? and

  • Are asynchronous requests possible?

will arise. While stackoverflow already abounds in explorations of these, I'll recommend http://www.ibm.com/developerworks/web/library/os-php-multitask/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜