Issue FORM POST Request From PHP using HTTP Basic Authentication

后端 未结 2 583
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 14:07

I hope this is a relatively straight forward thing to do, and that my google skills have simply failed me on this occasion. I have a BASIC authentication protected resource

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 14:36

    Here's a function I use to perform POST requests. Hopefully it can do what you want:

    function http_post($server, $port, $url, $vars) {
    
    // get urlencoded vesion of $vars array 
    $urlencoded = ""; 
    
    foreach ($vars as $Index => $Value) 
        $urlencoded .= urlencode($Index ) . "=" . urlencode($Value) . "&"; 
    
    $urlencoded = substr($urlencoded,0,-1);  
    
    $headers = "POST $url HTTP/1.0\r\n" 
    . "Content-Type: application/x-www-form-urlencoded\r\n" 
    . "Content-Length: ". strlen($urlencoded) . "\r\n\r\n"; 
    
    $fp = fsockopen($server, $port, $errno, $errstr, 10); 
    if (!$fp) return "ERROR: fsockopen failed.\r\nError no: $errno - $errstr"; 
    
    fputs($fp, $headers); 
    fputs($fp, $urlencoded); 
    
    $ret = ""; 
    while (!feof($fp)) $ret .= fgets($fp, 1024); 
    
    fclose($fp); 
    return $ret; }
    

    And here's an example of me using it to forward on the POST variables to an API

    $response = http_post("www.nochex.com", 80, "/nochex.dll/apc/apc", $_POST); 
    

提交回复
热议问题