2010-09-21 44 views
0

我将此函数与'fopen'一起使用,但我需要使用'curl'来代替。PHP - 将fopen更改为curl

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; 
} 

如果有人知道如何迁移,请发送您的答案!

+3

@ vote-closer:我觉得很清楚要问什么。 @Paulocoghi,如果您尝试通过阅读php curl http://www.php.net/curl手册中的示例并发布有关您尝试使用的任何问题的问题,可以获得更好的响应。 – 2010-09-21 17:51:41

+0

作为premiso sais。只要阅读'curl_setopt()'的所有选项,这很容易被重写。 – Wrikken 2010-09-21 18:11:01

回答

3

这是我用来发送后的任何URL,并得到结果:

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; 
    } 

我build_post_variables看起来是这样的:

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; 
    } 

传递在$ params数组应该在形式的$ params ['post_field'] ='价值';

就是这样。

0

看看PHP cURL page,因为它有许多应该帮助你的例子。