PHP-将fopen更改为curl
我将此函数与“fopen”一起使用,但我需要使用“curl”PHP-将fopen更改为curl,php,curl,fopen,Php,Curl,Fopen,我将此函数与“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
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;
}
如果有人知道如何迁移,请发送您的答案 看看,因为它有许多示例可以帮助您。看看,因为它有许多示例可以帮助您。这是我用来向任何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\u post\u变量如下所示:
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']='value'
就是这样。这是我用来向任何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\u post\u变量如下所示:
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']='value'
就这样。@请仔细投票:我认为问题很清楚@Paulocoghi,如果您自己尝试这样做,您会得到更好的回答,方法是阅读php curl手册中的示例,并针对您尝试的任何问题发布问题。只要通读
curl\u setopt()
的所有选项,这很容易重写。@请仔细投票:我想问题很清楚@Paulocoghi,如果您自己尝试这样做,您会得到更好的回答,方法是阅读php curl手册中的示例,并针对您尝试的任何问题发布问题。只要通读curl\u setopt()
的所有选项,就可以很容易地重写。