【问题标题】:PHP Curl upload file through Post DataPHP Curl 通过 Post Data 上传文件
【发布时间】:2010-10-15 04:00:01
【问题描述】:

我将如何提交与$data 中的此脚本位于同一目录中的文件。以下功能是我目前使用的。如果您在当前代码中发现任何错误,也请告诉我。

function post_data($site,$data){
    $datapost = curl_init();
 $headers = array("Expect:");
    curl_setopt($datapost, CURLOPT_URL, $site);
 curl_setopt($datapost, CURLOPT_TIMEOUT, 40000);
    curl_setopt($datapost, CURLOPT_HEADER, TRUE);
 curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($datapost, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($datapost, CURLOPT_POST, TRUE);
    curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
     curl_setopt($datapost, CURLOPT_COOKIEFILE, "cookie.txt");
    ob_start();
    return curl_exec ($datapost);
    ob_end_clean();
    curl_close ($datapost);
    unset($datapost);    
}

【问题讨论】:

    标签: php curl upload http-post


    【解决方案1】:

    您的第一个问题是您依赖 CURL, 开个玩笑,但这里还有另一种给这只猫剥皮的方法:

    // prepare post headers
    $opts = array(
        'http' =>
        array(
            'method' => 'POST',
            'charset' => 'utf-8',
            'timeout' => '60', // must be set for error handling
            'content' => 'post data can be inserted here', // this can be a file
           ));
    $context = @stream_context_create($opts);
    // send post data and store response data
    $response = @file_get_contents('http://www.example.org/file.php',
        false, $context);
    
    // using this posting method one does not have to rely on any external libraries
    

    【讨论】:

    • 可以 ...'content' =>... 是一条路径吗?还是必须成为文件内容?
    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 2016-02-22
    • 2017-10-25
    • 2016-03-19
    • 2015-04-02
    • 2011-08-04
    • 2019-02-27
    • 1970-01-01
    相关资源
    最近更新 更多