在 Guzzle 中复制 cURL post
Replicating cURL post in Guzzle
我使用 cURL post 将数据发送到 API,但我决定切换到 Guzzle。使用 cURL 我会这样做
$data =
"<Lead>
<Name>$newProject->projectName</Name>
<Description>$newProject->projectName</Description>
<EstimatedValue>$newProject->projectValue</EstimatedValue>
</Lead>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.someurl.com/lead.api/add?apiKey=12345&accountKey=12345");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'Content-Length: ' . strlen($data)
));
$output = curl_exec($ch);
这就是我目前正在尝试的 Guzzle。
$data = "<Lead>
<Name>$campaign->campaignName</Name>
<Description>$campaign->campaignName</Description>
<EstimatedValue>$campaign->campaignValue</EstimatedValue>
</Lead>";
$client = new GuzzleHttp\Client();
$req = $client->request('POST', 'https://somurl', [
'body' => $data,
'headers' => [
'Content-Type' => 'text/xml',
'Content-Length' => strlen($data),
]
]);
$res = $client->send($req);
$output = $res->getBody()->getContents();
我面临的第一个问题是它声明请求的参数 3 需要是一个数组,而我向它传递了一个字符串。很好,但是我怎样才能发送我的 xml 块呢?另外,我想我可能没有正确设置 headers?
我已经浏览了文档,看到参数 3 需要是一个数组,但我不知道如何 post 一个 XML 字符串。
感谢任何建议。
谢谢
您可以使用 'body' 参数创建数组:
$client->request('POST', 'http://whatever', ['body' => $data]);
阅读更多信息:http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=post#post-form-requests
要设置 headers,您可以这样做:
$response = $client->request('POST', 'http://whatever', [
'body' => $data,
'headers' => [
'Content-Type' => 'text/xml',
'Content-Length' => strlen($data),
]
]);
$output = $response->getBody()->getContents();
阅读更多信息:http://docs.guzzlephp.org/en/latest/request-options.html#headers
我使用 cURL post 将数据发送到 API,但我决定切换到 Guzzle。使用 cURL 我会这样做
$data =
"<Lead>
<Name>$newProject->projectName</Name>
<Description>$newProject->projectName</Description>
<EstimatedValue>$newProject->projectValue</EstimatedValue>
</Lead>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.someurl.com/lead.api/add?apiKey=12345&accountKey=12345");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'Content-Length: ' . strlen($data)
));
$output = curl_exec($ch);
这就是我目前正在尝试的 Guzzle。
$data = "<Lead>
<Name>$campaign->campaignName</Name>
<Description>$campaign->campaignName</Description>
<EstimatedValue>$campaign->campaignValue</EstimatedValue>
</Lead>";
$client = new GuzzleHttp\Client();
$req = $client->request('POST', 'https://somurl', [
'body' => $data,
'headers' => [
'Content-Type' => 'text/xml',
'Content-Length' => strlen($data),
]
]);
$res = $client->send($req);
$output = $res->getBody()->getContents();
我面临的第一个问题是它声明请求的参数 3 需要是一个数组,而我向它传递了一个字符串。很好,但是我怎样才能发送我的 xml 块呢?另外,我想我可能没有正确设置 headers?
我已经浏览了文档,看到参数 3 需要是一个数组,但我不知道如何 post 一个 XML 字符串。
感谢任何建议。
谢谢
您可以使用 'body' 参数创建数组:
$client->request('POST', 'http://whatever', ['body' => $data]);
阅读更多信息:http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=post#post-form-requests
要设置 headers,您可以这样做:
$response = $client->request('POST', 'http://whatever', [
'body' => $data,
'headers' => [
'Content-Type' => 'text/xml',
'Content-Length' => strlen($data),
]
]);
$output = $response->getBody()->getContents();
阅读更多信息:http://docs.guzzlephp.org/en/latest/request-options.html#headers