Skip to content

Instantly share code, notes, and snippets.

@RobertWang
Last active August 24, 2019 13:05
Show Gist options
  • Save RobertWang/9544062 to your computer and use it in GitHub Desktop.
Save RobertWang/9544062 to your computer and use it in GitHub Desktop.
[php]伪造host进行curl请求
<?php
/**
* 测试用例
* 用于处理需要绑定hosts的主机服务,比如某些接口调用等
*/
$apiurl = 'http://{$ServerRealIP}/weibo/send/mutil/'; // 修改原有的servername方式的url,使用真实服务器ip
$header = array('Host: {$ServerName}'); // 设置header中的伪造hosts主机地址
$data = array(
'param1' => 'value1',
'param2' => 'value2',
# ...
);
$ret = curl_post($apiurl, $data, $header);
print_r($ret);
function curl_post($url, $data = array(), $header = array(), $timeout = 5, $port = 80)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//curl_setopt($ch, CURLOPT_PORT, $port);
!empty ($header) && curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = array('status'=>true);
$result['result'] = curl_exec($ch);
if (0 != curl_errno($ch)) {
$result['error'] = "Error:\n" . curl_error($ch);
$result['status'] = false;
}
curl_close($ch);
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment