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);
?>
Related
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.");
}
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.
I am trying to get access token for user with credentials , that is registered in DB, with OAuth2.
In my oauth_clients I have a valid client with 'client_id=myclientid', 'client_secret=myclientsecret', 'grant_types=password'.
In my oauth_users table I have test user with 'username=Beno', 'password=aa888'.
I am sending data to 'http://myserver.com/token.php' like this
$ch = curl_init( 'http://myserver.com/token.php' );
curl_setopt( $ch, CURLOPT_HEADER, true);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(
'client_id' => 'myclientid',
'client_secret' => 'myclientsecret',
'grant_type' => 'password',
'username' => 'Beno',
'password' => 'aa888',
'u_id' => 53
) );
$auth = curl_exec( $ch );
on token.php I have this
<?php
if( file_exists("system/includes/autoload.php") ):
require_once("system/includes/autoload.php");
else:
require_once("../system/includes/autoload.php");
endif;
require_once('oauth2-server-php/src/OAuth2/Autoloader.php');
$dsn = 'mysql:dbname='.DATABASENAME.';host='.DBSERVERADDRESS.'';
// error reporting (this is a demo, after all!)
ini_set('display_errors',1);error_reporting(E_ALL);
// Autoloading (composer is preferred, but for this example let's just do this)
OAuth2\Autoloader::register();
// $dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost"
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => DBUSERNAME, 'password' => DBPASSWORD));
// Pass a storage object or array of storage objects to the OAuth2 server class
$server = new OAuth2\Server($storage);
// Add the "Client Credentials" grant type (it is the simplest of the grant types)
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
// Add the "Authorization Code" grant type (this is where the oauth magic happens)
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage));
$server->addGrantType(new OAuth2\GrantType\RefreshToken($storage));
$username = IO::post('username');
$password = IO::post('password');
$user_id = IO::post('u_id');
if ( ! empty( $username ) && ! empty( $password ) & ! empty( $user_id ) ){
$users = array( $username => array('user_id'=> intval($user_id) ,'password' => $password));
$clients = array($client_id => array('client_secret' => $client_secret));
// create a storage object
$storage = new OAuth2\Storage\Memory(array('user_credentials' => $users, 'client_credentials' => $clients));
echo "<pre>";
var_dump($storage);
echo "</pre>";
// create the grant type
$grantType = new OAuth2\GrantType\UserCredentials($storage);
// add the grant type to your OAuth server
$server->addGrantType($grantType);
// Handle a request for an OAuth2.0 Access Token and send the response to the client
$response = new OAuth2\Response();
$re = $server->handleTokenRequest(OAuth2\Request::createFromGlobals(),$response)->send();
echo $re;
}else{
echo "no data";
}
All data is in DB as I have mentioned above. But when I get response it returns me 400 error
{"error":"invalid_client","error_description":"The client credentials are invalid"}
Check how your authorization server receives client credentials.
You are presenting client credentials as form-post parameters, but your authorization server may expect that client credentials be embedded in Authorization header (Basic Authentication). Read "RFC 6749, 2.3.1. Client Password" carefully. According to the specification, "The authorization server MUST support the HTTP Basic authentication scheme for authenticating clients that were issued a client password." Therefore, embedding client credentials in Authorization header must work for any correct authorization server implementation.
I am trying to make a POST request using cURL in PHP. I have the code to make the POST request (from index.php) and I believe it is correct.
The next part is the API layer (api.php) which needs to extract the data from the POST request and this is where I am having issues. In the code, I am trying to read the value of the parameter q that I have passed using index.php.
Here's the code for both the files.
index.php
<?php
$handle = curl_init();
curl_setopt_array(
$handle,
array(
CURLOPT_URL => 'http://localhost:8888/restAPI/api.php',
'q' => 'getCompanyId',
'post_fields' => 'q=getCompanyId',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'q' => 'getCompanyId'
),
CURLOPT_RETURNTRANSFER => true
)
);
$response = curl_exec($handle);
curl_close($handle);
?>
api.php
<?php
require_once("Rest.inc.php");
class API extends REST {
public function processApi() {
$func = $_REQUEST['q'];
if((int)method_exists($this,$func) > 0){
$this->$func();
}
else{
$this->response('',404);
// If the method not exist with in this class, response would be "Page not found".
}
}
public function getCompanyId(){
$dbhost = 'localhost:8888';
$conn = mysql_connect($dbhost, 'root', 'root');
if (! $conn) {
die('Could not connect - ' . mysql_error());
}
$sql = 'SELECT companyId FROM Companies';
mysql_select_db('IRSocialBackend');
$executeSql = mysql_query($sql);
while($data = mysql_fetch_array($executeSql)){
echo $data['companyId'];
}
}
}
//echo "here";
$api = new API;
$api -> processApi();
?>
Just a side note: your API is not RESTFUL. REST is not a matter of "making HTTP requests". Read up on it!
First mistake:
array(
CURLOPT_URL => 'http://localhost:8888/restAPI/api.php',
'q' => 'getCompanyId',
'post_fields' => 'q=getCompanyId',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'q' => 'getCompanyId'
),
CURLOPT_RETURNTRANSFER => true
)
Random "q" and "post_fields" is definitely not how you add fields to curlopt.
In api.php, you assign the following:
$dbhost = 'localhost:8888';
$conn = mysql_connect($dbhost, 'root', 'root');
I thought localhost:8888 was your webserver? localhost:3306 would be your MySQL server if it is on a default port.
The rest is hard to debug without knowing your table/DB structure.
I'm using Ultimate Hosting package of GoDaddy. The account has a static IP and SSL installed. Now when I'm trying to use an API which needs static IP. But scripts are sending requests from random IPs. Please suggest me an way.
My Script
$soap_exception_occured = false;
$wsdl_path = 'http://vrapi.sslwireless.com/?wsdl';
$response = '';
ini_set('soap.wsdl_cache_enabled', '0'); // disabling WSDL cache
try {
$client = new SoapClient($wsdl_path);
}
catch(SoapFault $exception) {
$soap_exception_occured = true;
$response .= '\nError occoured when connecting to the SMS SOAP Server!';
$response .= '\nSoap Exception: '.$exception;
}
I'm using SOAP. Can IP binding help me ?
Assuming you are using curl of php to connect to that API, you should bind each request to your IP:
curl_setopt($ch, CURLOPT_INTERFACE, $myIP);
To bind CURL to a different outgoing network interface or a different IP address, all that is needed is to set the CURLOPT_INTERFACE to the appropriate value before executing the CURL request:
Try this and let me know what happend
$soap_exception_occured = false;
$ipandport = array(
'socket' => array(
'bindto' => 'xx.xx.xx.xx:port',
),
);
$setip = stream_context_create(ipandport);
$wsdl_path = 'http://vrapi.sslwireless.com/?wsdl';
$response = '';
ini_set('soap.wsdl_cache_enabled', '0'); // disabling WSDL cache
try {
$client = new SoapClient($wsdl_path, array('stream_context' => $setip));
}
catch(SoapFault $exception) {
$soap_exception_occured = true;
$response .= '\nError occoured when connecting to the SMS SOAP Server!';
$response .= '\nSoap Exception: '.$exception;
}
This thread will be a not complete without file_get_contents:
$opts = array(
'socket' => array(
'bindto' => 'xx.xx.xx.xx:0',
)
);
$context = stream_context_create($opts);
echo file_get_contents('http://www.example.com', false, $context);