This article introduces how to use Drupal cms to send a file through cURL Post.
As we all know, PHP cURL extensions can be used to simulate form submission. Drupal has the drupal_http_request function to execute an HTTP request. It can send a file via POST, but it is not as convenient as cURL. Here we will mainly explain how to Post a file to a remote server address in Drupal.
Webpage Form
The Code is as follows: |
Copy code |
<Form enctype = "multipart/form-data" method = "POST" url = "http://blog.lixiphp.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> |
The preceding form contains the text box, password, check box, and file submission.
Drupal cURL simulate form submission POST
The Code is as follows: |
Copy code |
<? Php $ 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' => 'lixip ', 'Password' => '000000 ′, 'Rememberme' => '1 ′, 'Avatar '=>' @ '. $ filename, ); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post ); $ Response = curl_exec ($ ch ); $ Response |
The value is the HTML output after the webpage Form is submitted.