コード例 #1
0
ファイル: multipost.class.php プロジェクト: limitium/uberlov
function multipost($url, $data, &$errno, &$errormessage, $timeout = 2, $headers = null)
{
    // Build post data
    $postdata = "--" . part::getBoundary() . "--\r\n";
    $postdatalen = strlen($postdata);
    for ($i = 0; $i < count($data); $i++) {
        $postdatalen += $data[$i]->getLength();
    }
    // identify server and location from URL
    $urlinfo = parse_url($url);
    $header = "POST " . $urlinfo['path'] . " HTTP/1.0\r\n";
    $header .= "Host: " . $urlinfo['host'] . "\r\n";
    if ($headers != null) {
        foreach ($headers as $name => $value) {
            $header .= $name . ':' . $value . "\r\n";
        }
    }
    $header .= "Content-type: multipart/form-data; boundary=" . part::getBoundary() . "\r\n";
    $header .= "Content-length: " . $postdatalen . "\r\n\r\n";
    if (isset($urlinfo['port'])) {
        $port = $urlinfo['port'];
    } else {
        $port = 80;
    }
    $fp = @fsockopen($urlinfo['host'], $port, $errno, $errstr, $timeout);
    # post the headers along with data
    $result = '';
    if ($fp) {
        fwrite($fp, $header);
        fwrite($fp, $postdata);
        for ($i = 0; $i < count($data); $i++) {
            $data[$i]->send($fp);
        }
        while (!feof($fp)) {
            $result .= fgets($fp, 1024);
        }
        # close the server connection
        fclose($fp);
        # extract real data from result by removing headers
        $begin = strpos($result, "\r\n\r\n");
        $result = substr($result, $begin + 4);
        # see the result
        return $result;
    } else {
        error_log('Failed to connect to:' . $urlinfo['host'] . ':' . $port . ' - error #' . $errno . ': ' . $errstr);
        return false;
    }
}