如何从php脚本以相同的方式(完全相同的行为)在浏览器中运行url时,我从地址栏运行url。我的意思是使用相同的标题数据,Cookie和浏览器发送的附加数据。如何在php中添加这些数据。如何运行:url from php =从浏览器运行
我需要这个原因,当我登录,从这个2箱子答案是不一样的:
- 在浏览器中我还是登录,这是正确的
- 从运行PHP我登出 - 不正确
我试过file_get_contents nod curl (from here)但它不能正常工作 - 响应仍然不同。
我打电话http://127.0.0.1/check.html
这里是功能检查:
public function check(){
echo 'begin';
// $total_rows = file_get_contents('https://127.0.0.1:8443/example.html?shopId=121');
$total_rows = $this->getUrl('https://127.0.0.1:8443/example.html', '121');
print_r($total_rows);
echo 'end';
}
function getUrl($url, $shopId ='') {
$post = 'shopId=' . $shopId;
$ch = curl_init();
$cookie_string="";
foreach($_COOKIE as $key => $value) {
$cookie_string .= "$key=$value;";
};
$cookie_string .= "JSESSIONIDSSO=66025D1CC9EF39ED7F5DB024B6026C61";
// echo $cookie_string;;
$ch = curl_init();
curl_setopt($ch,CURLOPT_COOKIE, $cookie_string);
// curl_setopt($ch, CURLOPT_PORT, 8443);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/../../files/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Secure Content-Type: text/html; charset=UTF-8"));
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: 127.0.0.1:8443'));
$ret = curl_exec($ch);
curl_error ($ch);
curl_close($ch);
return $ret;
}
您在使用上述任何方法时是否传递了正确的标题和会话/ cookie值以及任何其他参数。要获得正确的解决方案,请发布您尝试过的任何代码。只要陈述“这不起作用”并不能保证充分的回应。 '你有什么尝试'? –
不知道如何从浏览器中添加'正确的标题和会话/ cookie值'到php – studentttt
好的,你已经在上面的例子中使用了'CURLOPT_HEADER',并将其设置为0,类似于不发送头文件。 cookie有一个curl变量。请检查一下php manualand的例子,它会给你一个关于如何工作的想法。这可能会帮助你。 –