개발2015. 5. 4. 11:54
300x250




ㅁ PHP 에서 url 가져오는 방법 2가지



1. 우선 file_get_contents() 를 이용해서 url 가져오는 구문


<?php

$url = 'http://server.com/path';

$data = array('key1' => 'value1', 'key2' => 'value2');


// use key 'http' even if you send the request to https://...

$options = array('http' => array(

'method'  => 'POST',

'content' => http_build_query($data)

));

$context  = stream_context_create($options);

$result = file_get_contents($url, false, $context);


var_dump($result);

?>


2. 다음 curl 을 이용해서 url 가져오는 구문

<?php
// Define URL where the form resides
$form_url = "http://www.mogosselin.dev/example/simple-post.php";

// This is the data to POST to the form. The KEY of the array is the name of the field. The value is the value posted.
$data_to_post = array();
$data_to_post['username'] = 'Mickey';
$data_to_post['password'] = 'Minnie';

// Initialize cURL
$curl = curl_init();

// Set the options
curl_setopt($curl,CURLOPT_URL, $form_url);

// This sets the number of fields to post
curl_setopt($curl,CURLOPT_POST, sizeof($data_to_post));

// This is the fields to post in the form of an array.
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//execute the post
$result = curl_exec($curl);

//close the connection
curl_close($curl);

?>



300x250
Posted by 마스타