有几种方法可以在 PHP 中执行 HTTP 请求,在本教程中,我们将展示如何使用 filefunctions 与 stream_context_create 相结合来发送 POST 和 GET 请求。
虽然使用像 cURL 这样的库可能是执行 HTTP 请求最流行的方式之一,但我们也可以使用诸如 file_get_contents 和 fopen 之类的函数。
虽然这些函数的名称并没有完全表明它们也可以用于 HTTP 请求,但它们实际上在这方面工作得很好,而且它们也很容易使用。
前面提到的 stream_context_create 函数用于更好地控制与请求相关的各种选项。
使用内置函数代替 cURL 应该没有缺点,并且与通常声称的不同,它们可以用于 POST 和 GET 请求。
但是,一些托管可能会禁用 allow_url_fopen,这会破坏依赖这些方法的脚本。
POST 请求
发送 POST 请求并不比发送 GET 难多少。
我们只需要使用 stream_context_create 函数添加必要的标头来执行请求。
同样,我们可以使用 file_get_contents 和 fopen 来做到这一点;但让我们现在只使用 file_get_contents :
$sURL = "https://onitroad.com/Examples/http-post.php"; //The POST URL $sPD = "name=Jacob&bench=150"; //The POST Data $aHTTP = array( 'http' => //The wrapper to be used array( 'method' => 'POST', //Request Method //Request Headers Below 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $sPD ) ); $context = stream_context_create($aHTTP); $contents = file_get_contents($sURL, false, $context); echo $contents;
$sURL 变量包含 POST URL。
$sPD 变量包含我们要发布的数据。
格式应与内容类型标头匹配。
$aHTTP 数组具有所有选项,包括将传递给 stream_context_create 的标头。
我们还可以使用 fread 函数执行 POST 请求。
$sURL = "http://onitroad.com/Examples/http-post.php"; //The POST URL $sPD = "name=Jacob&bench=150"; //The POST Data $aHTTP = array( 'http' => //The wrapper to be used array( 'method' => 'POST', //Request Method //Request Headers Below 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $sPD ) ); $context = stream_context_create($aHTTP); $handle = fopen($sURL, 'r', false, $context); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); echo $contents;
请求头
如果我们有多个标题,请记住用 \r\n 分隔它们
content-type 标头告诉服务器发布的数据是如何格式化的。
web 应用程序经常使用 application/x-www-form-urlencoded 内容类型,但我们也可能遇到 multipart/form-data。
如果应用程序不支持我们指定的内容类型,则 POST 通常会失败。
内容标题包含我们要发布的实际数据。
这应该根据我们使用的内容类型进行格式化。
或者,我们也可以添加如下所示的选项和标题,这次添加一个引用标题:
$aHTTP['http']['method'] = 'GET'; $aHTTP['http']['header'] = "User-Agent: My PHP Script\r\n"; $aHTTP['http']['header'] .= "Referer: https://onitroad.com/\r\n";
另外的请求头会以同样的方式添加;通过在每个标题的末尾添加一个回车符 (\r) 和一个换行符 (\n),然后是我们要添加的任何标题来完成此操作。
添加完所有标头后,我们应该得到如下所示的内容(完整脚本):
$sURL = "https://onitroad.com/api/request-headers"; //The POST URL $aHTTP['http']['method'] = 'GET'; $aHTTP['http']['header'] = "User-Agent: My PHP Script\r\n"; $aHTTP['http']['header'] .= "Referer: https://onitroad.com/\r\n"; $context = stream_context_create($aHTTP); $contents = file_get_contents($sURL, false, $context); echo $contents;
获取请求
为了执行一个简单的获取请求,我们将首先创建一个 $handle 变量。
当使用 stream_get_contents 将下载的数据存储在 $contents 变量中时,我们可以使用它来引用请求。
$handle = fopen("https://onitroad.com/", "rb"); $contents = stream_get_contents($handle); fclose($handle);
stream_get_contents 函数会自动下载文件或者网页,但是,如果我们希望手动下载,为了更好地控制该过程,我们可以将 while 循环与 fread 结合使用:
$handle = fopen("https://onitroad.com/", "rb"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); echo $contents;
在这种情况下, fread 函数中的最后一个参数等于块大小,通常不大于 8192 (8*1024)。
请记住,这可以更大或者更小,但也可能受到运行 PHP 的系统的限制。
我们可以通过不使用比存储设备更大的块大小来优化 PHP 脚本。
下载文件的最简单方法之一是使用 file_get_contents 。
与使用其他方法相比,它需要的代码少得多,但它对过程的控制也较少。
$homepage = file_get_contents('https://onitroad.com/'); echo $homepage;