如何post通过curl函数在php文件? [英] how post a file via curl function in php?

查看:137
本文介绍了如何post通过curl函数在php文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个自动发布表单的脚本。
它不是垃圾邮件发送者!
在窗体中有一个图片字段。
我想用php和使用curl()函数写脚本。
如何实现文件上传?
并且是适合这个目的的php吗?我的意思是形式过帐?

解决方案

要上传你的服务器上的文件,是的,curl可以做的。 >

您需要使用 CURLOPT_POSTFIELDS 选项,将其传递到 curl_setopt 功能引用)


在HTTPPOST
操作中发布的完整数据。
要发布文件,
在文件名前加上 @ ,并使用
完整路径。




未测试,但我想这样的工作应该可以工作:

  $ ch = curl_init(); 
curl_setopt($ ch,CURLOPT_URL,http://www.yoursite.com/destination-of-upload.php);
curl_setopt($ ch,CURLOPT_HEADER,false);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setpopt($ ch,CURLOPT_POSTFIELDS,array(
'file'=>'@ ... / some-file.txt',//你需要修改
// some other fields?
));
$ result = curl_exec($ ch);
curl_close($ ch);


i want to write a script which post a form automatically. it is not a spammer! there is a picture field in form. i want to write the script with php and using curl() function. how can i implement file uploading? and is the php suitable for this purpose? i mean form posting?

解决方案

To upload a file that's on your server, yes, curl can do the trick.

You'll want to use the CURLOPT_POSTFIELDS option, passing it to the curl_setopt function (quoting) :

The full data to post in a HTTP "POST" operation.
To post a file, prepend a filename with @ and use the full path.


Not tested, but I suppose that something like this should work :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.yoursite.com/destination-of-upload.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setpopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => '@/..../some-file.txt',    // you'll need to adapt this
        // some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);

这篇关于如何post通过curl函数在php文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆