Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Guzzle中复制卷曲柱_Php_Laravel 5.2_Guzzle - Fatal编程技术网

Php 在Guzzle中复制卷曲柱

Php 在Guzzle中复制卷曲柱,php,laravel-5.2,guzzle,Php,Laravel 5.2,Guzzle,我曾使用cURL将数据发布到API中,但我决定改用Guzzle。使用cURL,我会这样做 $data = "<Lead> <Name>$newProject->projectName</Name> <Description>$newProject->projectName</Description> <EstimatedValue>$newProject->projectValue</E

我曾使用cURL将数据发布到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);
$data=
"
$newProject->projectName
$newProject->projectName
$newProject->projectValue
";
$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_头,false);
curl_setopt($ch,CURLOPT_HTTPHEADER,数组(
'内容类型:text/xml',
“内容长度:”.strlen($data)
));
$output=curl\u 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();
$data=”
$campaign->campaigname
$campaign->campaigname
$campaign->campaignValue
";
$client=new GuzzleHttp\client();
$req=$client->request('POST','https://somurl', [
“body”=>$data,
“标题”=>[
'内容类型'=>'文本/xml',
“内容长度”=>strlen($data),
]
]);
$res=$client->send($req);
$output=$res->getBody()->getContents();
我面临的第一个问题是,它说明请求的参数3需要是一个数组,而我正在向它传递一个字符串。这很好,但是如何发送xml块呢?此外,我想我可能设置的标题不正确

我已经阅读了文档,看到参数3需要是一个数组,但我不知道如何发布XML字符串

谢谢你的建议


谢谢

您可以使用“body”参数创建数组:

$client->request('POST', 'http://whatever', ['body' => $data]);
更多信息请访问:

要设置标题,可以执行以下操作:

$response = $client->request('POST', 'http://whatever', [
    'body' => $data,
    'headers' => [
        'Content-Type' => 'text/xml',
        'Content-Length' => strlen($data),
    ]
]);
$output = $response->getBody()->getContents();

更多信息,请访问:

$client->request('POST','http://whatever“,['body'=>$data])完美,谢谢。标记作为答案,我将接受。您知道如何设置头吗?#我尝试了类似的方法,但得到了类型错误:传递给GuzzleHttp\Client::send()的参数1必须实现接口Psr\Http\Message\RequestInterface@kate_hudson您不需要
$client->send
<代码>$client->request
本身会为您发出请求。见: