一聚教程网:一个值得你收藏的教程网站

热门教程

Drupal 通过cURL Post方式发送一个文件

时间:2022-06-24 23:07:44 编辑:袖梨 来源:一聚教程网

众所周知PHP的cURL扩展可以用来模拟表单提交。在Drupal中有drupal_http_request函数来执行一个HTTP请求,它可以通过POST方式来发送一个文件,但是使用起来没有cURL那么方便。 这里我们主要讲解如何在Drupal中Post一个文件到远程的服务器地址。

网页Form表单

 代码如下 复制代码


word” type=”password” value=”" />
checkbox” name=”rememberme” id=”" />

上面表单包含了演示文本框、密码、复选框和文件形式的提交。

Drupal cURL模拟表单提交POST

 代码如下 复制代码

$url = ‘http://blog.lixiphp.com/demo/http_request/post.php’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
‘username’ => ‘lixiphp’,
‘password’ => ’123456′,
‘rememberme’ => ’1′,
‘avatar’=> ‘@’.$filename,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);

$response

的值为网页Form表单提交后输出的HTML。

热门栏目