I use this script for send post and get file and his contents and process the request
$postdata = http_build_query(
array(
'inUsername' => ''.$_REQUEST['inUsername'].'',
'inPassword' => ''.$_REQUEST['inPassword'].'',
'csfr_token' => ''.$_REQUEST['csfr_token'].''
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result2 = file_get_contents('http://cp.ufowebs.com/index.php', false, $context);
$result="Invalid";
if (eregi($result,$result2))
{
print '{"login":"ok"}';
}
else
{
print "ok";
header("Location:http://cp.ufowebs.com/index.php");v<meta http-equiv="refresh" content="2;url=http://cp.controlpanel.com/index.php">
}
}
?>
In local or from other url works fine , but in the same url or if go to subdomain , etc , no works and give me this error always :
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/hostdata/admin/public_html/domain_com/test_cp.php on line 94 Warning: file_get_contents(http://cp.domain.com/index.php): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/hostdata/admin/public_html/domain_com/test_cp.php on line 94 ok
I donĀ“t know why works from other domain and no into the same domain , etc
file_get_contents from URL is blocked in most hosts. Consult with your hosting provider if it is blocked or not.
The domain does simply not exist.
nsa:~# host cp.ufowebs.com
Host cp.ufowebs.com not found: 3(NXDOMAIN)
This is not firewall related nor is it because a disabled functionality on your server.
ufowebs.com does exist. If you are the owner, Mr. Klaba, you have to setup a DNS record for the subdomain or setup * wildcard subdomain to match any subdomain traffic.
Related
In lumen my code is below to send file content in api response , for some document it is working fine but for some document it gives error for HTTP_INTERNAL_SERVER_ERROR
$content = base64_encode(file_get_contents($URL));
Symfony\Component\HttpKernel\Exception\HttpException Object
(
[statusCode:Symfony\Component\HttpKernel\Exception\HttpException:private] => 500
[headers:Symfony\Component\HttpKernel\Exception\HttpException:private] => Array
(
)
[message:protected] => HTTP_INTERNAL_SERVER_ERROR
[string:Exception:private] =>
[code:protected] => 0
[file:protected] => D:\xampp7.1\htdocs\PROJECT_NAME\app\Exceptions\Handler.php
[line:protected] => 76
[trace:Exception:private] => Array
( ...
Is there any solution or required to set php.ini veriable ??
In web error log it gives me error like ,
failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version not supported
to resolve this I have get file content with help of below,
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $data[$k]['doc_url']);
$data = curl_exec($ch);
curl_close($ch);
echo"<pre>";print_r($data);die;
it gives me empty output without file contect
I have also add below code
$content = base64_encode(file_get_contents(urlencode($data[$k]['doc_url'])));
$data[$k]['doc_content'] = "$content";
It gives me error of , failed to open stream: No such file or directory in PATH...
Could be that the URL your trying to reach is blocking such ways like you've tried to get contents from their server?
Maybe you should use a CURL with all the relevant settings.
The best way I know to duplicate a request is to open developers console on chrome, then on "Newtork" tab find the request that your trying to mimic. Right click -> Copy -> Copy as CURL (cmd), than paste it here to receive the equivalent PHP CURL code:
https://incarnate.github.io/curl-to-php/
hope it helps, if not, could you expan on the destination you're trying to reach?
good luck
First, you can set context to this kind of request:
$options = array(
'http' => array(
'protocol_version' => '1.0',
'method' => 'GET'
)
);
$context = stream_context_create($options);
Try to get the path to your file like this:
$api = "http://PATH_TO_YOUR_FILE?param=optional_query_params";
$response = file_get_contents($api, false, $context);
Good luck!
I've just noticed the new vulnerability discovered in Wordpress and I'm trying to fix it with the following code (but with any success
<?php
$url = 'https://mywebip/wp-login.php?action=lostpassword';
$data = 'user_login=admin&redirect_to=&wp-submit=Get+New+Password';
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Host: mailserver\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: ". strlen($data) ."\r\n",
'method' => 'POST',
'content' => $data,
'ssl'=>array('verify_peer'=>true, 'capath'=>'/etc/ssl/certs')
)
);
$context = stream_context_create($options);
//$result = file_get_contents($url, false, $context);
$fp = stream_socket_client($url, $errno, $errstr, 30);
//stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
$fp = fopen($url, 'r', false, $context);
if ($fp === FALSE) { /* Handle error */ }
var_dump($result);
?>
The error log I got is just like this:
PHP Warning: stream_socket_client(): unable to connect to https://mywebip/wp-login.php?action=lostpassword (Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?) in /home/jorge/Escritorio/joomla.php on line 18
PHP Warning: fopen(): Peer certificate CN=`website` did not match expected CN=`mywebip' in /home/jorge/Escritorio/joomla.php on line 21
PHP Warning: fopen(): Failed to enable crypto in /home/jorge/Escritorio/joomla.php on line 21
PHP Warning: fopen(https://mywebip/wp-login.php?action=lostpassword): failed to open stream: operation failed in /home/jorge/Escritorio/joomla.php on line 21
Where mywebip represents the actual ip that hosts my website and website and mailserver the DNS directions of the services.
Thank you.
Via socket you do not specify a protocol.
http://php.net/stream_socket_client
First parameter:
remote_socket
Address to the socket to connect to.
Adress is only mywebip.
You should use CURL instead.
See http://php.net/manual/en/curl.examples.php
The other problem (with fopen(), which can handle streams with protocols!) is a malformed/wrong certificate issued by your webserver.
Use this service to debug problems with your webservers certificate:
https://www.ssllabs.com/ssltest/
I have the script below and was wondering why I was getting the error:
failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required
The base64 encoded value is in the form username:password and is correct. The API documentation gave the example as:
Authorization: Basic
QWxhZGRpbjpvcGVuIHNlc2FtZQ==
For example, if the user agent uses Aladdin as the username and open sesame as the password, then the field is formed as above in the HTTP header
<?php
$postData = array(
'primaryContact' => array('id' => 1),
'subject' => 'Something not working'
);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\r\nContent-Type: application/json",
'content' => json_encode($postData)
)
));
// Send the request
$response = file_get_contents('http://127.0.0.1/', FALSE, $context);
// Check for errors
if($response === FALSE){
die('Error');
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData;
?>
Any ideas?
Tested and your code works. i think its the server. atleast check the server username and password again (your base64 code is correct)
I tested on: iis 7.5 the website has basic auth setup (http 401 Challenge)
(on iis that means that i can use my windows server credentials)
add the $http_response_headers to your error line to get more information.
// Check for errors
if($response === FALSE){
var_dump($http_response_header);
die('Error');
}
We have 2 servers, dev and live. They both use LAMP (Debian), but the dev server is regular http and the live server is https. This code works perfectly on our dev server:
$all_data = array(...);
$eol = "\r\n";
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($all_data)),
'connection: close'
);
$headers = implode($eol, $headers);
$opts = array(
'http'=>array(
'method' => 'POST',
'header' => $headers,
'content' => json_encode($all_data)
)
);
$context = stream_context_create($opts);
$update_data = file_get_contents($_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $system_file, null, $context);
On the live server, the file_get_contents gives this error:
Warning: file_get_contents(https://...): failed to open stream: Connection timed out in ....php on line 1086
I have looked at the apache logs for clues as to what is causing it to fail on the live server, but I only see the error above. It doesn't say what's blocking the call. I have allow_url_include and allow_url_fopen set to On on the live server.
I don't know if it's something in the Apache configuration or the SSL configuration, or something else, and I don't know where to look. I spent several hours today researching this issue and tried adding different headers to the headers array (User-Agent, Accept-Language, Accept-Encoding, Cookie, etc.), but nothing worked. I basically tried to replicate what was on dev, but live just didn't like it.
I also verified that if I copy and paste the URL that's being created directly in the browser that it works, so I know the filename is correct.
dev server:
php: 5.6.14-0+deb8u1
apache: Apache/2.4.10
debian: 8.2
live server:
php: 5.5.12
apache: Apache/2.4.10
debian: 7.9
Can you please use proxy and try
$aContext = array(
'http' => array(
'proxy' => 'tcp://192.168.0.2:3128',
'request_fulluri' => true,
),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("http://www.google.com", False, $cxContext);
echo $sFile;
At work we have to use a proxy to basically access port 80 for example, we have our own custom logins for each user.
My temporary workaround is using curl to basically login as myself through a proxy and access the external data I need.
Is there some sort of advanced php setting I can set so that internally whenever it tries to invoke something like file_get_contents() it always goes through a proxy? I'm on Windows ATM so it'd be a pain to recompile if that's the only way.
The reason my workaround is temporary is because I need a solution that's generic and works for multiple users instead of using one user's credentials ( Ive considered requesting a separate user account solely to do this but passwords change often and this technique needs to be deployed throughout a dozen or more sites ). I don't want to hard-code credentials basically to use the curl workaround.
To use file_get_contents() over/through a proxy that doesn't require authentication, something like this should do :
(I'm not able to test this one : my proxy requires an authentication)
$aContext = array(
'http' => array(
'proxy' => 'tcp://192.168.0.2:3128',
'request_fulluri' => true,
),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("http://www.google.com", False, $cxContext);
echo $sFile;
Of course, replacing the IP and port of my proxy by those which are OK for yours ;-)
If you're getting that kind of error :
Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 407 Proxy Authentication Required
It means your proxy requires an authentication.
If the proxy requires an authentication, you'll have to add a couple of lines, like this :
$auth = base64_encode('LOGIN:PASSWORD');
$aContext = array(
'http' => array(
'proxy' => 'tcp://192.168.0.2:3128',
'request_fulluri' => true,
'header' => "Proxy-Authorization: Basic $auth",
),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("http://www.google.com", False, $cxContext);
echo $sFile;
Same thing about IP and port, and, this time, also LOGIN and PASSWORD ;-) Check out all valid http options.
Now, you are passing an Proxy-Authorization header to the proxy, containing your login and password.
And... The page should be displayed ;-)
Use stream_context_set_default function. It is much easier to use as you can directly use file_get_contents or similar functions without passing any additional parameters
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);
?>
Depending on how the proxy login works stream_context_set_default might help you.
$context = stream_context_set_default(
array(
'http'=>array(
'header'=>'Authorization: Basic ' . base64_encode('username'.':'.'userpass')
)
)
);
$result = file_get_contents('http://..../...');
There's a similar post here: http://techpad.co.uk/content.php?sid=137 which explains how to do it.
function file_get_contents_proxy($url,$proxy){
// Create context stream
$context_array = array('http'=>array('proxy'=>$proxy,'request_fulluri'=>true));
$context = stream_context_create($context_array);
// Use context stream with file_get_contents
$data = file_get_contents($url,false,$context);
// Return data via proxy
return $data;
}