0
curl -X POST https://example.com/sandbox -u \
    'username:password' -d 'vendor=123456' -d 'list_id=1000001' \
    -H 'Accept: application/json

How would I structure a HTTP request with a command cURL like this, with a username/password?

Kevin
  • 185
  • 3
  • 15
  • http://stackoverflow.com/questions/3252851/how-to-display-request-headers-with-command-line-curl Run curl with the verbose flag and see what it generates. – Perry Tew Apr 13 '16 at 17:16

1 Answers1

2

You can use curl in PHP. I've created a example code for you:

$username='username';
$password='password';
$URL='https://example.com/sandbox';
$data=array('vendor'=>123456, 'list_id'=>1000001);

$payload = json_encode( $data );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type:application/json',
    'Accept: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$result=curl_exec($ch);
curl_close ($ch);

I hope that helps :D

Fabricio
  • 3,248
  • 2
  • 16
  • 22
  • Thanks, this is useful! Why did you put the key as "customer"? "customer"=> $data – Kevin Apr 13 '16 at 21:46
  • @Lanbo, sorry, it was just a typo, I've shared this code to you from a file that I used in site :P which send a big customer array to my service. Then I did some changed to match your cURL command. I've removed customer from it on the example. – Fabricio Apr 14 '16 at 14:40