반응형
file_get_contents를 사용하여 PHP에 데이터를 게시하려면 어떻게 해야 합니까?
PHP의 기능을 사용하고 있습니다.file_get_contents()
URL의 내용을 가져오고 변수를 통해 헤더를 처리합니다.$http_response_header
.
여기서 문제는 일부 URL에 데이터를 게시해야 한다는 것입니다(로그인 페이지 등).
그걸 어떻게 하는 거죠?
stream_context를 사용하면 할 수 있다는 것은 알고 있습니다만, 확실히는 알 수 없습니다.
감사합니다.
를 사용하여 HTTP POST 요구를 송신하는 것은 그다지 어렵지 않습니다.실제로 짐작하신 바와 같이$context
파라미터를 지정합니다.
이 페이지에는, PHP 메뉴얼의 예를 나타냅니다.HTTP 컨텍스트 옵션(인용):
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
기본적으로 적절한 옵션을 사용하여 스트림을 생성하여(그 페이지에 전체 목록이 표시됨) 세 번째 파라미터로 사용하여file_get_contents
--그 이상 없음;-)
사이드노트로서: 일반적으로 HTTP POST 요구를 송신하기 위해서, 많은 옵션을 제공하는 컬을 사용하는 경향이 있습니다.다만 스트림은 아무도 모르는 PHP의 장점 중 하나입니다.아쉽다...
또는 fopen을 사용할 수도 있습니다.
$params = array('http' => array(
'method' => 'POST',
'content' => 'toto=1&tata=2'
));
$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if (!$fp)
{
throw new Exception("Problem with $sUrl, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false)
{
throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}
$sUrl = 'http://www.linktopage.com/login/';
$params = array('http' => array(
'method' => 'POST',
'content' => 'username=admin195&password=d123456789'
));
$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if(!$fp) {
throw new Exception("Problem with $sUrl, $php_errormsg");
}
$response = @stream_get_contents($fp);
if($response === false) {
throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}
언급URL : https://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents
반응형
'programing' 카테고리의 다른 글
양방향 암호화:검색할 수 있는 암호를 저장해야 합니다. (0) | 2022.09.22 |
---|---|
특정 table.column을 참조하는 외부 키가 있고 외부 키에 대한 값이 있는 모든 테이블을 찾으려면 어떻게 해야 합니까? (0) | 2022.09.22 |
이삭이 나기 전에 Accessing Laravel .env 변수다. (0) | 2022.09.22 |
Java 스트림에서 flush()의 목적은 무엇입니까? (0) | 2022.09.22 |
Python에서 람다로 정렬하는 방법 (0) | 2022.09.22 |