2017-06-01 75 views
0

我希望能够验证cURL请求来自哪里。如何验证,允许和禁止域名通过curl访问php文件

所以,这里是客户会使用API​​:

$url_api = 'http://apiwebsite.com/api.php'; 
$post_data = array(
    'test' => 'test', 
    'test2' => 'test2', 
    'test3' => 'test3', 
    ); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url_api); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
$output = curl_exec($ch); 
if($output === FALSE){echo "cURL Error: " . curl_error($ch);} 
curl_close($ch); 
print_r($output); 

,然后在apiwebsite.com/api.php我需要的代码来验证请求的来源。

假设website1.com要求从代码通过卷曲apiwebsite.com/api.php

我如何可以验证该域名确实是website1.com?我怎样才能让api访问这个特定的域名?

通过邮寄或任何其他地方的客户代码发送不是我想要的,因为这可以很容易地打破/破解。我想直接从apiwebsite.com/api.php代码来验证请求API所访问的真正website1.com

这可能吗?

如果不是,我至少可以验证IP地址吗?或跟踪名称服务器之类的任何内容?

谢谢。

PS:卷曲是否安全传输密码?

+0

使用htaccess仅允许IP地址为唯一website1.com – ASR

回答

0

使用.htaccess允许api只有'website1.com'。你不需要检查你的api文件中的任何东西。

使用这个在您的htaccess文件

<files api.php> 
    order deny,allow 
    deny from all 
    allow from 0.0.0.0 (Ip for website1.com) 
    allow from 1.1.1.1 (Ip for your server) 
    allow from 1.1.1.1 (Ip for your work/personal/ network) 
    </files> 

最后两行是访问api.php自己。

+0

谢谢,没有关于它。 此代码将完美工作。 – Andre

相关问题