1

我想发布一些数据,包括图像。为此,我在 php 中使用 curl。我的代码如下 -

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, 'http://postacity.co.uk:8080/shine/pp/upload/');
    curl_setopt($ch, CURLOPT_POST, true);
    $post = array(
        'creatorid' => '119',
        'userfile' => '@/temp/fgf.jpg',
        'notice' => 'Image1',
        'catid' => '1',
        'title' => 'bookofzeus',
        'boardid' => '332',
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    if(curl_errno($ch)) {
        echo 'Error: ' . curl_error($ch);
    }
    else {
        echo $response;
    }
    }

每次我得到同样的错误

错误:创建表单数据失败

我做错了什么吗?我已经搜索过这个问题,但仍然没有找到解决方案。

4

2 回答 2

1

文件的路径是问题:'@/temp/fgf.jpg'

您应该看看这个错误说明:错误 50060 - 如果 post 数组值以 @ 开头,则无法创建 formpost 数据

这个问题的解决方案例如在phpfreaks

并使用文件的绝对路径。

于 2013-07-04T06:33:23.813 回答
1

参考这个它的工作

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">
    $post = array(
        "file_box"=>"@/path/to/myfile.jpg",
        "username"=>"foobar",
        "password"=>"secret",
        "submit"=>"submit"
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>

也参考这个网址

http://joshhighland.com/blog/2010/11/27/using-curl-and-php-to-upload-files-through-a-form-post/

http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/

于 2013-07-04T07:19:20.317 回答