/** * Do a GET request. * * The $data argument supports a special `_content` key * for providing a request body in a GET request. This is * generally not used, but services like ElasticSearch use * this feature. * * @param string $url The url or path you want to request. * @param array $data The query data you want to send. * @param array $options Additional options for the request. * @return \Cake\Http\Client\Response */ publicfunctionget($url, $data = [], array$options = []) { $options = $this->_mergeOptions($options); $body = null; if (isset($data['_content'])) { $body = $data['_content']; unset($data['_content']); } $url = $this->buildUrl($url, $data, $options);
/** * Do a POST request. * * @param string $url The url or path you want to request. * @param mixed $data The post data you want to send. * @param array $options Additional options for the request. * @return \Cake\Http\Client\Response */ publicfunctionpost($url, $data = [], array$options = []) { $options = $this->_mergeOptions($options); $url = $this->buildUrl($url, [], $options);
/** * Constructor * * Provides backwards compatible defaults for some properties. * * @param string $url The request URL * @param string $method The HTTP method to use. * @param array $headers The HTTP headers to set. * @param array|string|null $data The request body to use. */ publicfunction__construct($url = '', $method = self::METHOD_GET, array$headers = [], $data = null) { $this->validateMethod($method); $this->method = $method; $this->uri = $this->createUri($url); $headers += [ 'Connection' => 'close', 'User-Agent' => 'CakePHP' ]; $this->addHeaders($headers); $this->body($data); } ...
/** * Get/set the body/payload for the message. * * Array data will be serialized with Cake\Http\FormData, * and the content-type will be set. * * @param string|array|null $body The body for the request. Leave null for get * @return mixed Either $this or the body value. */ publicfunctionbody($body = null) { if ($body === null) { $body = $this->getBody();
在第 25 行的构造函数中使用 $this->body($data) 将 data 放在了 body 中,而在 body 函数中有一行代码判断 data 是否为数组,body 函数的文档中还提到了Array data will be serialized with Cake\Http\FormData, 也就是说如果是数组的话会 new 一个 FormData 对象,并且将 data 添加到 FormData 中
... /** * Add a new part to the data. * * The value for a part can be a string, array, int, * float, filehandle, or object implementing __toString() * * If the $value is an array, multiple parts will be added. * Files will be read from their current position and saved in memory. * * @param string|\Cake\Http\Client\FormData $name The name of the part to add, * or the part data object. * @param mixed $value The value for the part. * @return $this */ publicfunctionadd($name, $value = null) { if (is_array($value)) { $this->addRecursive($name, $value); } elseif (is_resource($value)) { $this->addFile($name, $value); } elseif (is_string($value) && strlen($value) && $value[0] === '@') { trigger_error( 'Using the @ syntax for file uploads is not safe and is deprecated. ' . 'Instead you should use file handles.', E_USER_DEPRECATED ); $this->addFile($name, $value); } elseif ($nameinstanceof FormDataPart && $value === null) { $this->_hasComplexPart = true; $this->_parts[] = $name; } else { $this->_parts[] = $this->newPart($name, $value); }
return$this; }
/** * Add multiple parts at once. * * Iterates the parameter and adds all the key/values. * * @param array $data Array of data to add. * @return $this */ publicfunctionaddMany(array$data) { foreach ($dataas$name => $value) { $this->add($name, $value); }
return$this; }
/** * Add either a file reference (string starting with @) * or a file handle. * * @param string $name The name to use. * @param mixed $value Either a string filename, or a filehandle. * @return \Cake\Http\Client\FormDataPart */ publicfunctionaddFile($name, $value) { $this->_hasFile = true;