2012-10-23 2 views
0

제목 자체 설명입니다. 여기 내 PHP 코드PHP에서 HTTP 처리기로 HTTP 게시 수행

function do_post_request($url, $data, $optional_headers = null) 
{ 
    $url = 'http://localhost:1181/WebSite1/PostHandler.ashx'; 
    $data = array('fprm' => 1, 'sprm'=> 2, 'tprm'=>3 
       ); 

    $params = array('http' => array(
       'method' => 'POST', 
       'content' => $data 
      )); 

//$params = array('method'=>'POST', 'content'=>$data); 
/* if ($optional_headers !== null) { 
    $params['http']['header'] = $optional_headers; 
    }*/ 
    $ctx = stream_context_create($params); 
    //stream_context_set_option() 

    //debug($params); 
    //die(); 
    $fp = @fopen($url, 'rb', false, $ctx); 
    if (!$fp) { 
    throw new Exception("Problem with $url, $php_errormsg"); 
    } 
    $response = @stream_get_contents($fp); 
    if ($response === false) { 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
    } 
    return $response; 
} 

이며, 여기 내 처리기 코드 :

public void ProcessRequest(HttpContext context) 
{ 
    context.Response.ContentType = "text/plain"; 
    string responseStr = "Hello World, this reply is from .net"; 
    string fprm = context.Request["fprm"]; 
    string sprm = context.Request["sprm"]; 
    string tprm = context.Request["tprm"]; 
    context.Response.Write(responseStr + " " + fprm + " " + sprm + " " + tprm); 

} 

이 내가 점점 오전 회신 :

이 매개 변수 값없이
'Hello World, this reply is from .net ' 

즉, 내가 읽은 A similar post 그리고 내가 가진 아이디어는 콘텐츠 매개 변수를 전달하기 위해 다른 컨텍스트 유형을 설정해야한다는 것입니다. 그러나 PHP 문서 throught를 찾고, 나는

어떤 도움이 좋을 것, 감사

+0

헤더 (content-type : 여기에 내용 유형); –

답변

1

당신은 당신의 스트림 컨텍스트에 content-type 헤더를 설정 잊고있는 상황에 맞는 유형을 설정, 어떤 옵션 http://php.net/manual/en/context.http.php을 찾을니까. application/x-www-form-urlencoded

로 설정하고 직접 스트림 컨텍스트에 대한 내용으로 배열을 전달할 수 없습니다, 그것은 urlencode되고 형식 문자열, 그래서

$params = array('http' => array(
       'method' => 'POST', 
       'header'=>'Content-Type: application/x-www-form-urlencoded', 
       'content' => http_build_query($data) 
      )); 

당신 didn를 이유를 http_build_query를 사용하는 PHP의 스트림 컨텍스트 문서에서 내용 유형을 변경하는 방법은 래퍼를 제공하지 않기 때문에 발생하지만 원하는 HTTP 헤더를 추가 할 수있는 방법을 제공합니다.

그렇지 않으면 요청한 서버 측 응용 프로그램이 정보 처리 방법을 모르는 문자열로 끝나기 때문에 필요합니다. 이는 토의 적으로 HTTP 요청의 내용에 대해 모든 종류의 데이터를 보낼 수 있기 때문입니다. application/x-www-form-urlencoded은 컨텐트로 전송 된 문자열이 직렬화 및 URL 인코딩 된 일반 html 형식임을 서버에 알립니다. http_build_query은 관계형 배열을 취해 html 형식과 같이 직렬화합니다.

+0

아니, dint work : ( – shabby

+0

업데이트 된 답변보기 – Delta

+0

빙고! got it, thanks – shabby