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 { ... }
?>
PHP/CURL - Making a POST request to another page and following the URL
How about this? It assumes name, amount and email will be posted to process.php
Added trouble shooting code.
process.php
<?php
//[put database queries here]
// $message = ??????????????????
$message = 'Message';
$post = array(
'name'=>$_POST['name'],
'amount'=>$_POST['amount'],
'email'=>$_POST['email'],
'message'=>$message);
$ch = curl_init('http://www.example.com/display.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
header('Content-Type: text/html');
$data = curl_exec($ch);
echo $data;
?>
This was tested and works too
does the redirect to the actual site.
<?php
$name = $_POST["name"];
$amount = $_POST["amount"];
$email = $_POST["email"];
$message = $_POST["message"];
echo <<< EOT
<html><head><style></style></head><body>
<form id="form" action="http://www.example.com/display.php" method="post">
<input type="hidden" name="name" value="$name"/>
<input type="hidden" name="amount" value="$amount"/>
<input type="hidden" name="email" value="$email"/>
<input type="hidden" name="message" value="$message"/>
</form>
<script>document.getElementById("form").submit();</script>
</body></html>
EOT;
?>
php curl post request and get result response
The very first: I'm using Firebug
add-on and see what happen
Here one ajax get content from other site
We have this link
https://apis.pos.com.my/apigateway/as2corporate/api/v2trackntracewebapijson/v1/?id=EP024922993MY&Culture=En
In request of header we can see require X-User-Key
Now we must find X-User-Key => We can view source
Now we build source code
<?php
function _curl($url,$post="",$usecookie = false,$_sock = false,$timeout = false,$x_user_key = false) {
$ch = curl_init();
if($post) {
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
}
if($timeout){
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
}
if($_sock){
curl_setopt($ch, CURLOPT_PROXY, $_sock);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12'
));
if($x_user_key){
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-User-Key: '.$x_user_key,
'Referer: http://poslaju.com.my/track-trace/',
'Origin: http://poslaju.com.my'
));
}
if ($usecookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
return $result;
}
function getStr($string,$start,$end){
$str = explode($start,$string,2);
$str = explode($end,$str[1],2);
return $str[0];
}
$url = 'http://poslaju.com.my/track-trace/';
$result_curl = _curl($url,'','','','','');
$x_user_key = getStr($result_curl,'{ "X-User-Key": "','" }');
$id_track = 'EP024922993MY';
$url = 'https://apis.pos.com.my/apigateway/as2corporate/api/v2trackntracewebapijson/v1/?id='.$id_track.'&Culture=En';
$result_curl = _curl($url,'','','','',$x_user_key);
echo $result_curl;
?>
Change id
on your mind
You can get json
content
Use print_r(json_decode($content_you_got)
And result you will have like this
Execute a HTTP POST Using PHP CURL
Managed to work out an answer to my question
<?php
$endpointUser = "mylogin";
//
$endpointPassword = "mypass";
//
$url = "https://dataurl.com";
$iframeUrl = "https://myurl.com?token=";
$fields = array(
"User.IdentityNumber"=> $_GET['security_phrase'] ,
"CapeConsumers.TrackerNumber"=> "6E273247DB4840G3",
"Call.AgentReference"=> $_GET['user'] ,
"Call.RecordingReference"=> $_GET['security_phrase']
);
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $endpointUser . ":" . $endpointPassword);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($process);
curl_close($process);
$token = str_replace('"','',$token);
?>
<iframe src="<?php echo $iframeUrl . $token; ?>" width="100%" height="800"></iframe>
I will leave the question unedited so that anybody else can have a look at how it was done if needed
How to do a PHP curl post
// init curl
$handle = curl_init();
// set options/parameters
curl_setopt( $handle, CURLOPT_URL, 'https://developer.lametric.c...');
curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $handle, CURLOPT_POSTFIELDS, 'the-json-encoded-data-here' );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true ); // you want to get the response
// set headers
curl_setopt( $handle, CURLOPT_HTTPHEADER, array( 'Accept: application/json',
'....' ) );
// execute the request and get the response
$response = curl_exec( $handle );
// get the status too
$status = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
// release resources
curl_close( $handle );
Just an example/introduction.
You initialize php's curl.
Setup all the parameters.
Send the request.
I won't write all the code for you.
PHP Reference is clear (and have examples too)
http://php.net/manual/en/book.curl.php
SO have examples too:
PHP + curl, HTTP POST sample code?
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
Send Python Output to HTML Field
How to Use Two Foreach to Insert Data in Database
Install PHP Intl Extension on Macos
Get Image Type from Base64 Encoded Src String
How to Change Value of Select Options Based on Another Select Option Selected
Getting a File to Download Instead of Opening the Browser
Mysql Return Result If 0 Rows Returned
Option Selected With Codeigniter and Data from Db
How to Get Username from Facebook Id
Laravel - Display a Pdf File in Storage Without Forcing Download
Update Database After Select Option Change