PHP, cURL, and HTTP POST example?
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
// Further processing ...
if ($server_output == "OK") { ... } else { ... }
?>
How can I use cURL to post form data in php?
since no answer got it right thus far (at least not with an approach that would work in php 5.6+), here goes: the equivalent php curl_ code would be:
$ch = curl_init ( 'https://apiprovider.com/api/v0/imports?token=[KEY]' );
curl_setopt_array ( $ch, array (
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array (
'file' => new CURLFile ( '/home/USERNAME/import.csv' )
)
) );
curl_exec ( $ch );
(i would also recommend setting CURLOPT_ENCODING to emptystring, especially if you expect the response to be compressible, that would be the equivalent of adding --compressed
to the curl command line, and might speed things up)
Related Topics
How to Send a Firebase Cloud Messaging Notification Without Use the Firebase Console
How to Truncate a String in PHP to the Word Closest to a Certain Number of Characters
How to Read If a Checkbox Is Checked in PHP
Why Can't I Access Datetime-≫Date in PHP'S Datetime Class
Mvc For Advanced PHP Developers
How to Explain Composer'S Error Log
How to Get a Substring Between Two Strings in PHP
Search Form With One or More (Multiple) Parameters
How to Get Id of the Last Updated Row in MySQL
How to Solve PHP Error 'Notice: Array to String Conversion In...'
Parse Error: Syntax Error, Unexpected End of File in My PHP Code
Gcm With PHP (Google Cloud Messaging)
Laravel Requires the Mcrypt PHP Extension
Invoking a PHP Script from a MySQL Trigger
MySQL VS MySQLi When Using PHP
PHP 7.2 Function Create_Function() Is Deprecated