php – 通过curl发送xml和标头
发布时间:2020-12-13 13:47:17 所属栏目:PHP教程 来源:网络整理
导读:想知道如何通过php在curl会话中设置所有这些数据: POST /feeds/api/users/default/uploads HTTP/1.1Host: uploads.gdata.youtube.comAuthorization: AuthSub token="DXAA...sdb8"GData-Version: 2X-GData-Key: key=adf15ee97731bca89da876c...a8dcSlug: vid
想知道如何通过php在curl会话中设置所有这些数据:
POST /feeds/api/users/default/uploads HTTP/1.1 Host: uploads.gdata.youtube.com Authorization: AuthSub token="DXAA...sdb8" GData-Version: 2 X-GData-Key: key=adf15ee97731bca89da876c...a8dc Slug: video-test.mp4 Content-Type: multipart/related; boundary="f93dcbA3" Content-Length: 1941255 Connection: close --f93dcbA3 Content-Type: application/atom+xml; charset=UTF-8 <?xml version="1.0"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <media:group> <media:title type="plain">Bad Wedding Toast</media:title> <media:description type="plain"> I gave a bad toast at my friend's wedding. </media:description> <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People </media:category> <media:keywords>toast,wedding</media:keywords> </media:group> </entry> --f93dcbA3 Content-Type: video/mp4 Content-Transfer-Encoding: binary <Binary File Data> --f93dcbA3-- 我不明白为什么有一些标题,然后–f93dcbA3更多标题(边界是什么?),一些xml(为什么在这里?),更多标题和文件内容. 我知道如何在没有xml部分和’boundary’的情况下发出请求. 任何帮助将不胜感激:D
边界是必需的,因为表单enctype是multipart / form-data,而在这种情况下是multipart / related.边界是一个唯一的字符串,不能出现在请求中的任何其他位置,它用于将每个元素与表单分开,无论是文本输入的值还是文件上载.每个边界都有自己的内容类型.
Curl不能为您执行multipart / related,因此您需要使用变通方法,请参阅curl邮件列表中的this message以获取建议.基本上,您必须自己构建大部分消息. 注意,最后一个边界有一个额外的 – 最后. 这段代码应该有助于您入门: <?php $url = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads'; $authToken = 'DXAA...sdb8'; // token you got from google auth $boundary = uniqid(); // generate uniqe boundary $headers = array("Content-Type: multipart/related; boundary="$boundary"","Authorization: AuthSub token="$authToken"",'GData-Version: 2','X-GData-Key: key=adf15....a8dc','Slug: video-test.mp4'); $postData = "--$boundaryrn" ."Content-Type: application/atom+xml; charset=UTF-8rnrn" .$xmlString . "rn" // this is the xml atom data ."--$boundaryrn" ."Content-Type: video/mp4rn" ."Content-Transfer-Encoding: binaryrnrn" .$videoData . "rn" // this is the content of the mp4 ."--$boundary--"; $ch = curl_init($url); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$postData); curl_setopt($ch,CURLOPT_RETURNTRANSFER,CURLOPT_FOLLOWLOCATION,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); $response = curl_exec($ch); curl_close($ch); 希望有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |