I am trying to post parameters from php to another server. When creating the link manually and opening it in the browser it works fine. But when trying to do it from my php-script it doesn't work. The file I am accessing is accessed, but the parameter is not posted.
I guess the problem has to do with how I define and post the parameter ($post_data .= "?companyid=banane";). What is my problem and how do I solve it?
<?php
$fp = fsockopen("192.168.1.102", 80, $errno, $errstr, 30);
error_log("write done");
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$post_data = "GET /cgi-bin/new_instance.pl HTTP/1.1\r\n";
$post_data .= "Host: 192.168.1.102\r\n";
$post_data .= "Connection: Close\r\n\r\n";
$post_data .= "?companyid=banane";
error_log("OUT - - - ".$post_data);
fwrite($fp, $post_data);
error_log("write done");
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
Or am I using the wrong approach? I'm thinking, if this is the correct approach then I should be able to find some good examples when googling around.
Try this as the first line instead:
GET /cgi-bin/new_instance.pl?companyid=banane HTTP/1.1\r\n
Related
I am making an application that deals with postbacks, and I wanted to make it so it would be able to postback to domains with https:// even if the person who is using the app doesn't have the openssl php extension. (It would warn them that their postbacks would be made non securely.)
I turned off openssl and tried the following, but it is giving me an error that I do not have https wrapper.
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
echo file_get_contents('https://httpbin.org/get?test=test', true, stream_context_create($arrContextOptions) );
Is it possible to make this request with file_get_contents?
Try this code:
<?php
$fp = fsockopen("ssl://somedomain/abc/", 2000 , $ErrNo, $ErrString, 30);
if (!$fp) {
echo "Error No : $ErrNo - $ErrString <br />\n";
} else {
$out = "POST / HTTP/1.1\r\n";
$out .= "Host: somedomain \r\n";
$out .= "Content-Type: application/xml; charset=utf-8;\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
I have research file_get_contents with --no-check-certificate
readmore here file_get_contents ignoring verify_peer=>false?
I have the following (stripped-down) piece of code:
function curl_request_async($url, $params)
{
foreach ($params as $key => $val) {
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
fwrite($fp, "$type ".$parts['path']." HTTP/1.1\r\n");
fwrite($fp, "Host: ".$parts['host']."\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($post_string)."\r\n");
fwrite($fp, "Connection: Close\r\n\r\n");
$bytes_written = fwrite($fp, $post_string);
var_dump($bytes_written, strlen($post_string));
// fread($fp, 1);
// fflush($fp);
fclose($fp);
}
The problem with this code is that I found no evidence the request reached the server called. The line var_dump($bytes_written, strlen($post_string)); outputted int(493) int(493), so it should have received all data, yet it didn't.
If I uncomment fread($fp, 1); it works without a problem. That could be working solution, but it doesn't seem to make sense. There has to be a better way!
My question then is two-fold: why does fread($fp, 1); fix my problem and is there a better solution?
your problem is probably that you wrote the server code in PHP, and you dont have ignore_user_abort=true by default (see http://php.net/manual/en/misc.configuration.php#ini.ignore-user-abort ), so when you close the connection, your server stop executing your php code, thus fread(fp,1) fix your problem - connection dont close before php start writing a response
you can use this code to make a server to test if its actually connecting or not -
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$sck=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($sck===FALSE){
die('socket_create failed!');
}
if(!socket_set_block($sck)){
die("socket_set_block failed!");
}
if(!socket_bind($sck, '0.0.0.0',1337)){
die("FAILED to bind to port 1337");
}
if(!socket_listen($sck,0)){
die("socket_listen failed!");
}
$fullFile='';
while((print('listening for connections!'.PHP_EOL)) && false!==($conn=socket_accept($sck))){
echo "new connection!".PHP_EOL;
echo "generating crypto iv..";
while(false!==($buffi=socket_recv($conn,$buff,1024,MSG_WAITALL))){
if($buffi===0){
break;//socket_recv's way of
//saying that the connection closed,
//apparently. the docs say it should return
// false, but it doesn't, it just infinitely returns int(0).
// at least on windows 7 x64 sp1.
}
$fullFile.=$buff;
echo "recieved ".strlen($fullFile)." bytes...".PHP_EOL;
$buff='';//do i need to clear it? or wiill recv do it for me?
}
echo "all bytes recieved (i guess, todo, socket_last_error confirm).";
echo PHP_EOL;var_dump($fullFule);
echo "done!".PHP_EOL;
}
die("should never reach this code...");
it will make a netcat-style server on http://127.0.0.1:1337
fread needs two parameters: a resource and a length number of bytes to read.
Right now you are only reading 1 byte. fread($fp, 1);
If you want to read the complete result, loop it until readed completely:
while(!feof($fp)){
echo fread($fp, 128);
}
I am working on twitter login integration with website. I don't have cURL installed in my server and I am not allowed to install that.
Twitter code is working fine for login. But while using request_token curl is used to send callback URL with that URL and getting the token response. In this same case I want to get the response from that URL without using Curl in PHP. Is it possible?
Curl code now used:
$response = curl_exec($ci);
The above response I need without using Curl.
How to get the response from url without Curl
You don't have to necessarily use cURL, there can be many ways. One of those is:
$response=file_get_contents($ci);
Edit:
You can also use fsockopen, here is an example from PHP.net
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
I'm writing a proxy server in PHP. The proxy server works and is able to render HTML, but JavaScript code will not work. Based on research I've done, many other proxy servers also have problems with JavaScript.
Here is the code I'm using.
#set_time_limit(60);
$fp = fsockopen(".$page.", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: ".$hostname."\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$x=0;
while (!feof($fp)) {
echo fgets($fp, 128);
fclose($fp);
}
}
Help would be greatly appreciated.
i need to telnet to a port and send command and write the output into a txt file using PHP.How i do it?
in this forum have a same question name telnet connection using PHP but their have a solution link and the solution link is not open so i have to make the question again.
Also i try the code below from php site but it does not save the proper output into a text file.Code:
<?php
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: localhost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
So,please help me to solve the problem.How i telnet to localhost port 80 and send command GET / HTTP/1.1 and write the output into a text file?
With a simple additition, your example script can write the output to a file, of course:
<?php
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: localhost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$output = '';
while (!feof($fp)) {
$output .= fgets($fp, 128);
}
fclose($fp);
file_put_contents( 'output.txt', $output );
}
Then again, I agree with Eduard7; it's easier not to do the request manually and just let PHP solve it for you:
<?php
// This is much easier, I imagine?
file_put_contents( 'output.txt', file_get_contents( 'http://localhost' ) );
You really want to do this with telnet? What about:
echo file_get_contents("http://127.0.0.1:80");
Or if You want to customize the request, you can use cURL - http://php.net/manual/en/book.curl.php