如何通过PHP发送POST / GET请求/数据

PHP 4.3.0 saw the introduction of the Streams API, which provides a consistent set of methods for reading and writing data to and from any specific location. Whenever you use file_get_contents() or another filesystem function, you are actually using the Streams API, albeit transparently in the background. A “stream” is simply a resource object which can be read from or written to. Streams might include:

PHP 4.3.0引入了Streams API,它提供了一套一致的方法来读写特定位置的数据。 每当您使用file_get_contents()或其他文件系统函数时,您实际上都在使用Streams API,尽管在后台是透明的。 “流”只是可以从中读取或写入的资源对象。 流可能包括:

  • A file on a local filesystem

    本地文件系统上的文件

  • An HTTP connection to a website

    与网站的HTTP连接

  • An FTP connection to a server

    与服务器的FTP连接

Streams API covers literally everything – reading, writing, renaming and deleting. As you will probably know, the unlink() or rmdir() functions are used to delete files and folders on a filesystem. What you might not know is these use the Streams API. Why, you ask? These filesystem operations can be performed on files and folders that reside on both local and remote filesystems (e.g. deleting a directory from an FTP server using rmdir()). HTTP and FTP connections are not one and the same, but it would make sense if they can share a common set of functions that naturally perform operations common to one another. That’s the Streams API in a nutshell.

Streams API实际上涵盖了所有内容-读取,编写,重命名和删除。 您可能会知道,unlink()或rmdir()函数用于删除文件系统上的文件和文件夹。 您可能不知道这些使用Streams API。 你为什么问? 可以在本地和远程文件系统上的文件和文件夹上执行这些文件系统操作(例如,使用rmdir()从FTP服务器删除目录)。 HTTP和FTP连接不是相同的,但是如果它们可以共享一组自然地执行彼此共有的操作的通用功能,这将是有意义的。 简而言之,这就是Streams API。

So now we have that out of the way, let’s see how we can include POST/GET data within an HTTP request. As you might expect, we use the file_get_contents() function as usual, but there’s a small difference – we need to create our own stream context. In plain English, the stream context in an HTTP request essentially consists of the header information and some options for things like whether to ignore failure status codes, etc). Creating a custom context simply modifies the default options which would otherwise be used for the stream.

现在,我们已经解决了这个问题,让我们看看如何在HTTP请求中包含POST / GET数据。 如您所料,我们像往常一样使用file_get_contents()函数,但有一个小的区别–我们需要创建自己的流上下文。 用简单的英语来说,HTTP请求中的流上下文本质上由标头信息和一些选项(例如是否忽略故障状态代码等)组成。 创建自定义上下文只需修改默认选项,否则这些默认选项将用于流。

And with that, let’s jump right into the code:

这样,让我们​​直接进入代码:

$url = 'http://www.example.com';
 
$data = http_build_query( array( ‘name’ => ‘value’ ) );
 
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded",
        'method'  => 'POST',
        'content' => $data,
    ),
);
 
$context = stream_context_create( $options );
 
$result = file_get_contents( $url, false, $context );

那么这是怎么回事? 让我们看一下每一行: (So what’s going on here? Let’s look at each line:)

  • First, we need to create an array with the POST data we want to send with our HTTP request. We use the http_build_query() function to convert our array into a URL-encoded format ?like=this. You can’t send arrays in HTTP requests, of course.

    首先,我们需要使用要通过HTTP请求发送的POST数据创建一个数组。 我们使用http_build_query()函数将数组转换为URL编码格式?like = this 。 当然,您不能在HTTP请求中发送数组。

  • The content type you can see within the $options array is the default for forms.

    您可以在$ options数组中看到的内容类型表单的默认类型

  • Create our custom stream context with the $options we created.

    使用我们创建的$ options创建我们的自定义流上下文。
  • Fetch the page using file_get_contents(), but using the stream context we just created. This ensures PHP sends out the HTTP request with our POST data.

    使用file_get_contents()获取页面,但使用我们刚刚创建的流上下文。 这样可以确保PHP将POST数据与HTTP请求一起发送出去。

翻译自: https://www.eukhost.com/blog/webhosting/how-to-send-postget-requestsdata-via-php/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值