[php] PHP의 URL에 데이터 POST

양식없이 PHP의 URL에 POST 데이터를 보내려면 어떻게해야합니까?

양식을 완성하고 제출하기 위해 변수를 보내는 데 사용할 것입니다.



답변

html 형식을 사용하지 않고 PHP 코드 자체에서 URL에 데이터를 게시하려는 경우 curl을 사용하여 수행 할 수 있습니다. 다음과 같이 표시됩니다.

$url = 'http://www.someurl.com';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

그러면 게시물 변수가 지정된 URL로 전송되고 페이지에서 반환하는 내용은 $ response에 있습니다.


답변

cURL-less 당신은 php5에서 사용할 수 있습니다

$url = 'URL';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
        'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);


답변

귀하의 질문은 명확하지 않지만 양식을 사용하지 않고 POST 데이터를 URL로 보내려는 경우 fsockopen 또는 curl을 사용할 수 있습니다.

다음 은 둘 다에 대한 꽤 좋은 연습입니다.


답변