Cahya Darma Wijaya
Cahya Darma Wijaya

Reputation: 43

How to make a http requests as a curl post?

I have done some HTTP tamper data and trying to make my own curl post, but I'm afraid that I can't understand how these thing work, can anyone explain how do I put it on? based on my trustworthy tamper data on firefox there are three phases on submit data on this website, first is

URL : http://www.thisiswebsite.xyz/Nginx/script/order_handler.php
Method  POST
Type    xmlhttprequest
itemname : IFHPB-P14    
orderstep : 1

and after that a HTTP Headers i think

URL : http://www.thisiswebsite.xyz/Nginx/script/order_handler.php
Method  POST
Type    xmlhttprequest
Host : www.thisiswebsite
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0
Accept : text/html, */*; q=0.01
Accept-Language : en-US,en;q=0.5
Accept - Encoding : gzip, deflate
Referer : http://www.thisiswebsite/Nginx/index.php
Content-Type : application/x-www-form-urlencoded; charset=UTF-8 
X-Requested-With : XMLHttpRequest
Content-Length  : 30
Connection  : keep-alive
Cookie  : PHPSESSID=6gjfhn0475l26oanag1bugs025

and finally is the submit post data

URL : http://www.thisiswebsite.xyz/Nginx/script/order_handler.php
Method  POST
Type    xmlhttprequest
itemname : IFHPB-P14
orderstep : 3
username : testing
hdsn : datatest 
MAC  : datatestmac

My try with curl

curl -X POST http://www.thisiswebsite.xyz/Nginx/script/order_handler.php \ -H 'Host: www.thisiswebsite.xyz' \ -H 'Connection: keep-alive' \ -H 'Accept: text/html, */*; q=0.01' \ -H 'Accept-Language: en-US,en;q=0.5' \ -H 'Accept - Encoding: gzip, deflate' \ -H 'Referer: http://www.thisiswebsite.xyz/Nginx/index.php' \ -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \ -H 'X-Requested-With: XMLHttpRequest' \ -H 'Content-Length: 30' \ -H 'Cookie: PHPSESSID=6gjfhn0475l29oanagdbugs022' \ -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0' \ --data "itemname=IFHPB-P4&orderstep=3&username=cahya&hdsn=123&MAC=123"

Question: how do I make this 3 HTTP Data to a simple curl post?

Upvotes: 2

Views: 117

Answers (1)

ivanivan
ivanivan

Reputation: 2225

Build your data as an associative array to make the key/value pairs.

Build your headers as an array.

Initialize curl, set the required options, execute, capture output, shut down curl, print results.

<?php

// build your data as an associative array for the keys
// and yes, you can use multi-dimensional arrays, etc
$data=array();
$data['item']="abc123";
$data['orderstep']=3;
$data['username']="joe.user";
$data['hsdn']=545;
$data['MAC']="bigmac";

// you can set options for various headers as needed, just
// do all of them  as an array()
$headers=array();
$headers[]="Accept: text/html,*/*";
$headers[]="Referer: http://some.example.com";
$headers[]="Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
// and so on...

// set the URL for your POST to go to
$url="http://api.example.com/end/point";

// now initialize curl
$ch=curl_init();
// set the options for your headers,
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
// and http method
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
// do you want to capture any returned output from server?
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
// what URL to call
curl_setopt($ch,CURLOPT_URL,$url);
// what data to send
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));
// make it so!
$curl_result=curl_exec($ch);
// done with curl
curl_close($ch);
// show results
print_r($curl_result."\n");

?>

Upvotes: 1

Related Questions