문제

나는이 일을하는 방법을 알아 내려고 몇 시간 동안 고군분투하고 있습니다. http-put을 통해 파일을 exist db로 보내려고합니다. 서버에 대한 사용자 인증이 있으므로 다음과 같은 작업을 수행하려고했습니다.

문서를 넣을 곳이있는 URL이 있습니다. exist의 사용자 이름과 비밀번호가 있습니다.

Curl로 작업하려고 시도했지만 조용히 실패했지만 PHP 스트림을 사용하려고했지만 "Error 201/Created"를 계속 얻었지만 실제로 파일이 생성되지 않았습니다.

이에 대한 도움은 크게 감사 할 것입니다.

다음은 PHP 스트림을 사용해 보았던 샘플 코드입니다.

        $data = file_get_contents($tmpFile);                                                                                                    
         $header = array(
             "Authorization: Basic " . base64_encode($this->ci->config->item('ws_login') . ':' . $this->ci->config->item('ws_passwd')),
             "Content-Type: text/xml"
         );  
         $params = array(
             'http' => array(
                 'method' => 'PUT',
                 'header' => $header,
                 'content' => $data));
         $ctx = stream_context_create($params);

         $response = file_get_contents($url, false, $ctx);
도움이 되었습니까?

해결책

아하! 여기 책상에 심술쟁이 난쟁이 박제 인형으로 약간의 "고무 오리 킹"이후, 나는 해결책을 알아 냈습니다.

        $data = file_get_contents($tmpFile);
         $params = array(
             'http' => array(
                 'method' => 'PUT',
                 'header' => "Authorization: Basic " . base64_encode($this->ci->config->item('ws_login') . ':' . $this->ci->config->item('ws_passwd')) . "\r\nContent-type: text/xml\r\n",
                 'content' => file_get_contents($tmpFile)
             )
         );
         $ctx = stream_context_create($params);
         $response = @file_get_contents($url, false, $ctx);

         return ($response == '');

다른 팁

컬이 나를 위해 작동합니다. 여기 내 코드의 스 니펫이 있습니다.

                $handle = curl_init ($server_url);

                if ($handle)
                {
                    // specify custom header
                    $customHeader = array(
                        "Content-type: $file_type"
                    );
                    $curlOptArr = array(
                        CURLOPT_PUT => TRUE,
                        CURLOPT_HEADER => TRUE,
                        CURLOPT_HTTPHEADER => $customHeader,
                        CURLOPT_INFILESIZE => $file_size,
                        CURLOPT_INFILE => $file,
                        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                        CURLOPT_USERPWD => $user . ':' . $password,
                        CURLOPT_RETURNTRANSFER => TRUE
                    );
                    curl_setopt_array($handle, $curlOptArr);
                    $ret = curl_exec($handle);
                    $errRet = curl_error($handle);
                    curl_close($handle);

편집 : 방금 내 코드를 업데이트했습니다. 나는 직접 인증을 사용하지 않으므로 테스트되지 않았습니다.

이것은 나를 위해 작동합니다 ...

function put($_server,$_file,$_data)
{
  $fp = @fsockopen ($_server, 80, $errno, $errstr, 30);
  if ($fp)
  {
    $p = "PUT $_file HTTP/1.0\r\n";
    $p.= "User-Agent: Mozilla/3.0 (Windows NT 5.0; U) Opera 7.21  [da]\r\n";
    $p.= "Host: $_server\r\n";
    $p.= "Accept: text/html, application/xml;q=0.9, application/xhtml+xml;q=0.9, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\r\n";
    $p.= "Accept-Language: da;q=1.0,en;q=0.9\r\n";
    $p.= "Accept-Charset: windows-1252, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1\r\n";
    $p.= "Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0\r\n";
    $p.= "Referer: http://www.nasa.gov/secret/flightplans.asp\r\n";
    $p.= "Content-type: application/x-www-form-urlencoded\r\n";
    $p.= "Content-length: ".strlen($_data)."\r\n";
    $p.= "\r\n";
    $p.= $_data;

    //echo($p);
    fputs ($fp, $p);
  }
  else die("dagnabbit : $errstr");

  while ($l=fgets($fp))
    echo($l);
  fclose($fp);
}

많은 헤더 라인은 아마도 필요하지 않을 것입니다 ... 그러나 CouchDB와 대화 할 때 작동하여 제조를하지 않았습니다.

기존 DB에 SOAP 인터페이스가 활성화 된 경우 오픈 소스 라이브러리가 호출됩니다. PHEXIST 데이터베이스와 쉽게 상호 작용할 수 있습니다.

function _publish($service, $doc) {
    $params = array(
        'http' => array(
            'method' => 'PUT'));
    $context = stream_context_create($params);
    $fp = fopen($service, 'rb', false, $context);
    $response = fwrite($fp,file_get_contents($doc));
    if ($response === false) {
        return false;
    }
    // Pull out the status code from the header
    $metaData = stream_get_meta_data($fp);
    preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/", $metaData['wrapper_data'][0], $matches);
    $code = end($matches[1]);
    if ($code == 200) {
        return true;
    } else {
        return false;
    }
}

~에서http://www.littlehart.net/atthekeyboard/2008/01/11/how-to-http-put-a-file-some-some-using-php/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top