【问题标题】:Dialog IBM - Curl - PHP Laravel对话框 IBM - Curl - PHP Laravel
【发布时间】:2016-03-28 14:26:44
【问题描述】:

Watson 有下一个代码:

 curl -u "{username}":"{password}" 
  -X POST
  --form file=@template.xml
  --form name=templateName
  "https://gateway.watsonplatform.net/dialog/api/v1/dialogs"

我有:

$ch = curl_init();
    $data['name'] = $this->post['name'].";type:form";
    $data['file'] = $this->getCurlValue($this->post['file'], $this->post['nombre']);

    curl_setopt($ch, CURLOPT_URL, $this->url.$this->metodo);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");

    if(count($this->post) > 0){
        //curl_setopt($ch, CURLOPT_PUT, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    json_decode($output);

$this->帖子有...

$data = Request::only('name', 'file');

我只得到答案:{}

我错过了什么?

谢谢大家!...第一次尝试使用 curl 发送上传文件:c

【问题讨论】:

  • 我用 PHP 尝试过,但我找不到工作。在终端中制作卷曲非常容易。另外,我尝试使用“@”,buuut,不推荐使用“@”的卷曲文件......

标签: php laravel curl dialog


【解决方案1】:

这比我想象的要简单。

商店:

$data = Request::only('name', 'file');

$nombre = time().".".Request::file('file')->getClientOriginalExtension();
Request::file('file')->move(base_path() . '/public/dialogos/', $nombre);

$data['path'] = base_path() . '/public/dialogos/'.$nombre;
$data['file'] = $_FILES['file'];

卷曲:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.$this->metodo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");

if(count($this->post) > 0){
    $datos['name'] = $this->post['name'];
    $datos['file'] = $this->getCurlValue($this->post['file'], $this->post['path']);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $datos);
}
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output);

getCurlValue($file, $path);

if (function_exists('curl_file_create')) {
    return curl_file_create($path, $file['type'], $file['name']);
}
$value = "@{$path};filename=" . $file['name'];
$value .= ';type=' . $file['type'];

return $value;

愚蠢的消息说我需要 PUT 或 DELETE 方法...哈哈

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    相关资源
    最近更新 更多