简体   繁体   English

在 PHP cURL 帮助中传递凭据

[英]passing credentials in PHP cURL help

I am trying to pass credentials to a website so I can use file_get_contents on it to extract some data but it is not working, I am getting a blank page so any idea what is wrong here?我正在尝试将凭据传递给网站,以便可以在其上使用 file_get_contents 来提取一些数据,但它不起作用,我得到一个空白页,所以知道这里有什么问题吗?

<?php


$username="munged@ring.gil.com";
$password="Koin";

$url="confluence.rogersdigitalmedia.com";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

$str= file_get_contents("confluence.rogersdigitalmedia.com/display/prodsupport/Team+Calendar");
echo $str;
?>

Here is the new code it is still not working stuck at login screen when I do get contents....这是新代码,当我确实获得内容时,它仍然无法工作卡在登录屏幕上......截屏

<?php
$username="munged@gil.ro.com";
$password="Koin";

$url="confluence.rogersdigitalmedia.com";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


//Replaced due to special chars in url for username and pass
//curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_USERPWD, urlencode($username) . ':' . urlencode($password));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo file_get_contents('http://confluence.rogersdigitalmedia.com/exportword?pageId=1114407');
?>

New code: I know $url is the URL for which I have to login, but what do I put in $data ?新代码:我知道$url是我必须登录的 URL ,但是我在$data中输入了什么? I know it's my login info, but how do I put it (eg, <username> space <password>)?我知道这是我的登录信息,但我该如何输入(例如,<username> 空格 <password>)?

<?php
function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

You probably need to escape the @ in the username:您可能需要转义用户名中的@

 curl_setopt($ch, CURLOPT_USERPWD, urlencode($username). ':'. urlencode($password));

This is because some characters (such as @ and : ) have special meanings in URLs.这是因为某些字符(例如@: )在 URL 中具有特殊含义。 You need to escape them so that the server will treat them as the characters, rather than as indicating something about the HTTP request.您需要对它们进行转义,以便服务器将它们视为字符,而不是指示有关 HTTP 请求的内容。

Scrap all that.报废这一切。 Your edit shows that you have a fundamental misapprehension about how HTTP works.您的编辑表明您对 HTTP 的工作原理存在根本性的误解。 You can't use CURL to pass HTTP login credentials to a site that expects a web form to be filled out and then expect to be able to log in with file_get_contents (which is part of a completely separate API).您不能使用 CURL 将 HTTP 登录凭据传递到需要填写 web 表单然后希望能够使用单独的file_get_contents登录的站点。

In terms of how you actually do this... First, find out whether the site has an API.就您实际执行此操作的方式而言...首先,确定该站点是否有 API。 If it does not, find out whether they actually want you to be screen-scraping.如果没有,请查明他们是否真的希望您进行屏幕抓取。 It would be very bad form to screen-scrape a website that doesn't allow it.对不允许的网站进行屏幕抓取是非常糟糕的形式。 It may well in fact be a breach of copyright law.事实上,这很可能违反了版权法。

Second, program to the API.其次,对 API 进行编程。 This means logging in in the way that the site enforces.这意味着以网站强制执行的方式登录。 This will be with form values (which you will need to send in CURLOPT_POSTFIELDS , probably) and cookies (which you will need to use in every request, probably using CURLOPT_COOKIEFILE ).这将包含表单值(您可能需要在CURLOPT_POSTFIELDS中发送)和 cookies (您需要在每个请求中使用它,可能使用CURLOPT_COOKIEFILE )。

Work at understanding how the client-server relationship works, study the API and don't just chuck code at the problem and expect it to work.努力理解客户端-服务器关系是如何工作的,研究 API,不要只是在问题上扔代码并期望它能够工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM