Drupal 通过cURL Post方式发送一个文件
本文章来给大家介绍一个Drupal 通过cURL Post方式发送一个文件实现,如果你正在使用Drupal cms不防进入参考一下哦。
众所周知PHP的cURL扩展可以用来模拟表单提交。在Drupal中有drupal_http_request函数来执行一个HTTP请求,它可以通过POST方式来发送一个文件,但是使用起来没有cURL那么方便。 这里我们主要讲解如何在Drupal中Post一个文件到远程的服务器地址。
网页Form表单
<form enctype="multipart/form-data" method="POST" url="http://blog.zhengshuiguang.com/demo/http_request/post.php"> <input name="username" type="text" value="" /> <input name="password" type="password" value="" /> <input type="checkbox" name="rememberme" id="" /> <input name="avatar" type="file" /> </form>
上面表单包含了演示文本框、密码、复选框和文件形式的提交。
Drupal cURL模拟表单提交POST
<?php $url = 'http://blog.zhengshuiguang.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。
文章地址:http://www.phprm.com/develop/54592.html
转载随意^^请带上本文地址!