Basic Authentication & Header Location - php

I am performing an HTTP request to populate an iframe by php.
Basically, I don't know how to make the basic authentication in the same redirection.
I send the authentication in the header but the page is always asking for credentials with the famous popup -> http://prntscr.com/j0fao9
Code below
$username = "suzy";
$password = "password";
$remote_url = 'http://10.10.10.215:8080/pentaho/api/repos/%3Apublic%3ASteel%20Wheels%3ADashboards%3ACTools_dashboard.wcdf/generatedContent';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
);
$context = stream_context_create($opts);
function Redirect($remote_url, $context)
{
header('Location: ' . $remote_url, false, $context);
exit();
}
Redirect( $remote_url, false, $context);
Probably, the problem is in the header sentence, any suggestions?
Tks in advance!

have you tried using http://username:password#domain.com/...
that way your passing the username and password thru the url.

Related

Recaptcha error code 'connection-failed' on verification

When implementing recaptcha v2, I am given the error code 'connection-failed' when trying to verify the recaptcha input.
I have followed this (https://www.freakyjolly.com/how-to-add-google-recaptcha-in-php-form/) tutorial as I had no luck with others that I found
require('src/autoload.php');
$siteKey = 'my key';
$secret = 'my key';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$gRecaptchaResponse = $_POST['g-recaptcha-response'];
$remoteIp = $_SERVER['REMOTE_ADDR'];
$recaptchaErrors = '';
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
$error[] = "worked";
} else {
$recaptchaErrors = $resp->getErrorCodes();
foreach($recaptchaErrors as $err)
{
$error[] = $err;
}
}
I have not had much luck finding any details on this error anywhere, and it is not documented on the official recaptcha page. I have edited the snippet above for testing purposes, but it would be sending an email.
If allow_url_fopen is off in your php.ini, the connection will fail because Recaptcha uses file_get_contents to access the API by default. I would not enable this flag as it can pose a security risk.
My suggestion, if you have the php curl module installed, is to use Recaptcha with a curl connection:
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
I have had the same problem while working locally in a node environment running node-php-awesome-server.
If you are trying to verify the reCaptcha response from localhost, with a localhost reCaptcha key pair, try from a live webserver (with relative key pair) instead.
For some reason sending the request from localhost returned me that error.
I suppose it has something to do with the development environment but did not investigate further.
I've had the same problem when i tried to include recaptcha in my website on localhost, i then tried this code on my live website(on the server) and it worked, hope this helps.
$secret = 'your server side key from google';
$post_data = http_build_query(
array(
'secret' => $secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']));
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data));
$context = stream_context_create($opts);
$response =file_get_contents('https://www.google.com/recaptcha/api/siteverify',false, $context);
$result = json_decode($response);
if($result->success){
echo "Success";
}
if (!$result->success) {
echo "CAPTCHA verification failed.");
}

Linkedin api oauth2.0 get user profile details

As per the new oauth 2.0 for linkedin.
I am unable to get user profile details.
I have set the attributes in my app but still cant get the details.
Can somebody help me the code snippet to get user details.
public function fetch($method, $resource, $body = '') {
$opts = array(
'http' => array(
'method' => $method,
'header' => "Authorization: Bearer " .
$_SESSION['oauth_access_token'] . "\r\n" .
"x-li-format: json\r\n"
)
);
$url = 'api.linkedin.com' . $resource;
if (count($this->$params)) {
$url .= '?' . http_build_query($this->$params);
}
$context = stream_context_create($opts);
$response = file_get_contents($url, false, $context);
return json_decode($response);
}
$xml_response = $LinkedInn->fetch("GET","/v1/people/~");
Please check your permissions in your code when you are logging in with linked-in.
Look at this link-
https://developer.linkedin.com/support/developer-program-transition

How to get reports from Commision Junction API - PHP

Commision Junction is the name of an affiliate company. I am not familiar with SOAP, WSDL and with web services in general, but wanted to quickly test the data coming back from their affiliate api. Cannot make it work though. They provide a page for their API
I tried smtg like:
public function testCJApi() {
$url = "http://" . $this->user . ":" . $this->password . "#datatransfer.cj.com/datatransfer/files/" . $this->account . "/outgoing/commission_report.csv";
$xml = simplexml_load_file($url);
if (isset($xml)) {
return ($xml
? $this->formatJsonReturn($xml, array("txt"=>"CJ Results OK","code"=>""))
: $this->formatJsonReturn("", array("txt"=>"CJ Results Empty","code"=>""))
);
}
}
but it didn't give me any results. I just need to quickly test the data coming back.
The API link they provide is
http://api.affiliatewindow.com/v4/MerchantService?wsdl.
I have figured it out myself:
public function testCJApi() {
$uri = "https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2013-02-15&end-date=2013-02-17"; // can be other api uri, this is one of them
$context = stream_context_create(
array(
'http' => array(
'method' => 'GET',
'header' => 'Authorization: ' . 'YOUR API KEY GOES HERE'
)
)
);
$x = file_get_contents($uri, false, $context);
$response = new SimpleXMLElement($x);
return $response->asXML();
}

How to use get_headers when I am behind a proxy server?

There don't seem to have any code to set the proxy server and its authentication.
Use stream_context_set_default function.
This blog post explains how to use it. Here is the code from that page.
<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234"; // Proxy server port
$PROXY_USER = "LOGIN"; // Username
$PROXY_PASS = "PASSWORD"; // Password
// Username and Password are required only if your proxy server needs basic authentication
$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
array(
'http' => array(
'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",
'request_fulluri' => true,
'header' => "Proxy-Authorization: Basic $auth"
// Remove the 'header' option if proxy authentication is not required
)
)
);
$url = "http://www.pirob.com/";
print_r( get_headers($url) );
echo file_get_contents($url);
?>

how do i send a message/notification with the linkedin api?

i have an application that authenticates users through the linkedin api:
is it possible for an application to send messages to all users who authorized it? (ie: the app's system notifications)
is it possible to send a message to a subset of the application users? (ie: gold members etc. you can assume i have all the linkedin IDs stored somewhere)
i've been looking for a while, and can't find anything/
Something like this
function message($subject, $body, $recipients)
{
if (!is_array($recipients)) {
throw new Exception('Recipients must be suplied as an array');
}
// Start document
$xml = new DOMDocument('1.0', 'utf-8');
// Create element for recipients and add each recipient as a node
$elemRecipients = $xml->createElement('recipients');
foreach ($recipients as $recipient) {
// Create person node
$person = $xml->createElement('person');
$person->setAttribute('path', '/people/' . (string) $recipient);
// Create recipient node
$elemRecipient = $xml->createElement('recipient');
$elemRecipient->appendChild($person);
// Add recipient to recipients node
$elemRecipients->appendChild($elemRecipient);
}
// Create mailbox node and add recipients, body and subject
$elemMailbox = $xml->createElement('mailbox-item');
$elemMailbox->appendChild($elemRecipients);
$elemMailbox->appendChild($xml->createElement('body', ($body)));
$elemMailbox->appendChild($xml->createElement('subject', ($subject)));
// Append parent node to document
$xml->appendChild($elemMailbox);
$response = fetch('POST','/v1/people/~/mailbox', $xml->saveXML());
return ($response);
}
function fetch($method, $resource, $body = '') {
$params = array('oauth2_access_token' => $_SESSION['access_token'],
'format' => 'json',
);
// Need to use HTTPS
$url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
// Tell streams to make a (GET, POST, PUT, or DELETE) request
$context = stream_context_create(
array('http' =>
array('method' => $method,
'header'=> "Content-Type:text/xml\r\n"
. "Content-Length: " . strlen($body) . "\r\n",
'content' => ($body)
)
)
);
// Hocus Pocus
$fp = fopen($url, 'r', false, $context);
$response = file_get_contents($url, false, $context);
$result =json_decode($response,true);
return $result;}
message('Subject', 'body', array('id'));
function fetch from Code Sample
The only messaging supported by the API is via the Messaging API, which only allows for messages to be sent from one connection to another... so in theory, you (as in you the developer, not the application itself, as it has no connections) could send a message to any of your applications user's that you also happen to be connected to in some way. The Messaging API is pretty clear though that messages must be triggered by a specific action, and the maximum number of recipients is 10.
So the short answer is, not possible, although the above may be a bit of a workaround. An alternative would be to ask the user directly for their email address once they have completed the LI process and then you can contact them as you wish without running into API limits/restrictions.

Categories