Reputation: 14949
I use this function with 'fopen', but I need to use 'curl' instead.
function rest_auth_xml($url,$method,$token,$chave,$xml)
{
$auth = sprintf('Authorization: Basic %s',base64_encode("$token:$chave") );
$opts = array(
'http'=>array(
'method'=>$method,
'header'=>"Content-Type: text/xml\r\n" .
$auth."\r\n",
'content'=>$xml
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen($url, 'r', false, $context);
$conteudo = stream_get_contents($fp);
fclose($fp);
return $conteudo;
}
If anyone knows how to migrate, send your answer!
Upvotes: 0
Views: 2847
Reputation: 5726
This is what I use to send post to any URL and get results:
public static function get_page_with_url_and_parameters($url, $params) {
$query_params = self::build_post_variables($params);
$curl_handler = curl_init($url);
curl_setopt($curl_handler, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handler, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl_handler, CURLOPT_POST, TRUE);
curl_setopt($curl_handler, CURLOPT_POSTFIELDS, $query_params);
curl_setopt($curl_handler, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl_handler, CURLOPT_HEADER, FALSE);
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl_handler);
curl_close($curl_handler);
return $output;
}
My build_post_variables looks like this:
private static function build_post_variables($params)
{
$post_string = '';
foreach ($params as $key => $value)
{
$key = urlencode($key);
$value = urlencode($value);
$post_string .= $key.'='.security::xss_clean($value).'&';
}
$post_string = rtrim($post_string, '&');
return $post_string;
}
The $params array you pass in should be in the form of $params['post_field']='value';
That's it.
Upvotes: 3
Reputation: 85308
Take a look at the PHP cURL page as it has many examples that should help you out.
Upvotes: 0