2

Working on a CURL Post to a restful backend service. Using RESTClient extension in Firefox I get my intended response.

So I have my Authorization, Accept, and Content-Type Headers set in CURL

$ch = curl_init();  
curl_setopt_array(
    $ch, 
    array(
        CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
        CURLOPT_URL => 'http://myurl.com/restfulservice/',
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $jsonpoststring,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
        CURLOPT_USERPWD => sprintf('%s;%s', 'foo@bar.com', 'somepassword'),
        CURLOPT_SSL_VERIFYPEER => false
    )
);
$output = curl_exec($ch);
curl_close($ch);

In RESTClient I have this JSON in the Body

{"authentication":{"identification":{"identstring":"foo@bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}

My question is: How to include that body in the PHP Curl request.

Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
  • What is `$jsonpoststring` set to? Usually you want that to be an associative array in the form of `variablename => stringvalue`. – amphetamachine Dec 17 '14 at 17:27
  • possible duplicate of [PHP cURL, POST JSON](http://stackoverflow.com/questions/4271621/php-curl-post-json) – Matt K Dec 17 '14 at 17:27

2 Answers2

0

Pass it in postfields

jsonpoststring='{"authentication":{"identification":{"identstring":"foo@bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
';
$ch = curl_init();  
curl_setopt_array(
    $ch, 
    array(
        CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
        CURLOPT_URL => 'http://myurl.com/restfulservice/',
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $jsonpoststring,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
        CURLOPT_USERPWD => sprintf('%s;%s', 'foo@bar.com', 'somepassword'),
        CURLOPT_SSL_VERIFYPEER => false
    )
);
$output = curl_exec($ch);
curl_close($ch);
Kunal Pradhan
  • 522
  • 1
  • 5
  • 12
0

This should do the trick

$data = json_decode('{"authentication":{"identification":{"identstring":"foo@bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}');

$ch = curl_init();  
curl_setopt_array(
    $ch, 
    array(
        CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
        CURLOPT_URL => 'http://myurl.com/restfulservice/',
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
        CURLOPT_USERPWD => sprintf('%s;%s', 'foo@bar.com', 'somepassword'),
        CURLOPT_SSL_VERIFYPEER => false
    )
);
$output = curl_exec
Jelle Keizer
  • 723
  • 5
  • 9