cURL to Guzzle转换

时间:2015-07-08 12:13:16

标签: post curl guzzle

我有一个运作良好的cURL:

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

并且想使用Symfony2 Guzzle的多卷曲

到目前为止我尝试了什么:

$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);

但这不起作用,我得到了一个我不应该得到的数组。

1 个答案:

答案 0 :(得分:1)

您可以尝试将授权移至cURL。

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

X是密码,但通常不需要密码,所以你只需添加一个X(随机字符)