Saul Ortega Saul Ortega - 5 years ago 478
PHP Question

Translate a PHP cURL to a bash cURL call

I have this PHP code that makes a curl call:

$postData['api_user'] = 'username';
$postData['api_key'] = 'password!';

$ch = curl_init('https://api.example.com/send.json');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));


I know that in order to make a curl call with json in bash I have to do this:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource


But I'm confused in how I can transfer all my php code to bash curl call, what I mean is for example what would be the equivalent of curl_setopt(), and to pass the credentials as an array like I did with http_build_query()

Answer Source

Use cURL to POST your data as JSON.

curl -i \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-X POST --data '{"api_user": "username", "api_key":"password!"}' \
--insecure \
https://api.example.com/send.json

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); equals -k, --insecure.

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData))

equals -X POST --data ...

CURLOPT_RETURNTRANSFER... hmm, i don't know ,)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download