PHP的HTTP非常低级请求

时间:2010-12-27 15:08:16

标签: php http api

我在想如何在PHP中编写一个函数,它将采用单个字符串参数,如下所示:

function executeHTTP($cmd)
{
   //some way to execute $cmd
}

现在$ cmd就是它所包含的......

$cmd = "
POST /action/GetUploadToken HTTP/1.1
Host: gdata.youtube.com
Authorization: AuthSub token="DXAA...sdb8"
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Content-Length: 1941255
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>
"

知道我该怎么做?

4 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

您应该查看cURL扩展程序:http://nl3.php.net/curl

答案 2 :(得分:1)

使用fsockopen,正如zoldar所说,你可以发出一个原始的HTTP请求。

function executeHTTP($cmd,$host="gdata.youtube.com",$port=80)
{
    $fp = fsockopen($host, $port, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
        return false;
    } else {
        fwrite($fp, $cmd);
        $data = fread($fp);
        fclose($fp);
    }
    return $data;
}

您还需要指定主机和端口,但您可以设置默认值。

答案 3 :(得分:1)

为什么不使用YouTube's PHP SDK