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?

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is useful! Why did you put the key as "customer"? "customer"=> $data
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.