Milos Cuculovic
Milos Cuculovic

Reputation: 20223

cURL to Guzzle conversion

I have a cURL that works well:

curl -H "Authorization: Token 71e24088d13304cc11f5a0fa93f2a2356fc43f41" -H "Content-Type: application/json" -X POST -d 
'{
  "reviewer": {"name": "test name", "email": "[email protected]"}, 
  "publication": {"title": "test title", "doi": "xxx"},
  "complete_date": {"month": 6, "year": 2015}
}' 
    https://my.url.com/ --insecure

And would like to use the multi curl from Symfony2 Guzzle

What I tried so far:

$client = new \Guzzle\Http\Client();
$request = $client->post('https://my.url.com/');

$jsonBody = "'{ ";
$jsonBody .= '"reviewer": {"name": "'.$review['name'].'", "email":"'.$review['email'].'"}, ';
$jsonBody .= '"publication": {"title": "';

if ($review['article_title'] != '')
    $jsonBody .= $review['article_title'];
else
    $toExec .= $review['manuscript_title'];

if ($review['doi'] != '')
    $jsonBody .= '", "doi": "'.$review['doi'].'"}, ';
else
    $toExec .= '"}, ';

$jsonBody .= '"complete_date": {"month": '.$month.', "year": '.$year.'}';
$jsonBody .= "}' ";

$options = [
        'headers'   => [ 'Content-Type' => 'application/json', 'Authorization' => 'Token 71e24088d13304cc11f5a0fa93f2a2356fc43f41' ],
        'body'      => $jsonBody,
];


$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);

$response = $client->post('-d', $options);

But this doesn't work, I am getting an array as a result which I am not supposed to.

Upvotes: 2

Views: 309

Answers (1)

a-am
a-am

Reputation: 431

You might try moving your authorization to cURL.

$token = '71e24088d13304cc11f5a0fa93f2a2356fc43f41';
$request->getCurlOptions()->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$request->getCurlOptions()->set(CURLOPT_USERPWD, "$token:X");

X is the password but often there is none needed so you just add an X (random character)

Upvotes: 1

Related Questions