在PHP中有多种进行HTTP请求的方法, 本文中介绍最经常使用的三种:php
1) 文件流的方式:file_get_contents , 这种方式是PHP自带的。android
2)cURL方式: cURL是PHP的一个第三方库 , 目前PHP4.0以上都自带cURL lib.web
3) PECL_HTTP 扩展: 这是一个PECL的extension, 须要安装才能使用数据库
1. 文件流的方式: PHP的文件流自己就支持HTTP协议,所以咱们能够象使用文件同样来进行HTTP的操做
这种方式适合比较简单的HTTP GET 或 POST请求, 但对于一些复杂的应用场景中的HTTP 请求仍是有些力不从心的。app
if (!isset($_GET["tx"])) { header("Location: http://www.cubebackup.com"); exit() ; } $post_array = array ( "cmd" => "_notify-synch", "tx" => $_GET["tx"], "at" => PDT_IDTOKEN ); $post_string = http_build_query($post_array); $opts = array( 'http' => array( 'method' => "POST", 'header' => "Content-Type: application/x-www-form-urlencoded", 'content'=> $post_string ) ); $context = stream_context_create($opts); $pdt_response = file_get_contents(PAYPALURL, false, $context); if ($pdt_response === FALSE) { header("Location: http://www.cubebackup.com"); exit(); } $response_array = preg_split("/\s+/" ,$pdt_response); $pdt_data = array(); if ($response_array[0] === "SUCCESS") { foreach ($response_array as $value) { $pdt_pair = explode('=', $value); if (isset($pdt_pair[1])) { $pdt_data[$pdt_pair[0]] = urldecode($pdt_pair[1]); } //这里能够对数据进行处理,好比写入数据库,或者显示给用户等。。 } } else { header("Location: http://www.cubebackup.com"); exit(); }