【问题标题】:Posting to PHP script that then uses curl to post text/file data to external site发布到 PHP 脚本,然后使用 curl 将文本/文件数据发布到外部站点
【发布时间】:2011-08-09 00:06:29
【问题描述】:
我有一个包含几个文本字段和一个文件上传字段的表单。
<form enctype="multipart/form-data" method="POST">
<input name="source" type="file">
<input name="message" type="text" value="">
</form>
我需要将文本输入和文件输入发布到自身。然后获取发布的数据,并使用 curl 将其发布到 facebook 的 graph api。
这可能吗?如果是这样,任何通过 curl 发送文件数据的代码示例将不胜感激。谢谢!
【问题讨论】:
标签:
php
post
curl
multipartform-data
【解决方案1】:
想通了。这是解决方案:
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $_POST['accesstoken'];
$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, $graph_url);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="source">
$post = array(
"source"=>"@".$_FILES['source']['tmp_name'],
"message"=>$_POST['meessage']
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
}
?>