fwrite not working with some additional non-special characters - php

I have a script to send post to an url. The complete code doesn't work for the following example, it does not show any error
<?php
$posturl = 'https://sub1.domain.com/mailforward.php';
$message_array = array();
$message_array['From'] = 'WordPress <wordpress#sub2.domain.com>';
$message_array['To'] = 'me#domain.com';
$message_array['Subject'] = '[My Blog] Password Reset';
$message_array['Body'] = 'Someone has requested a password reset for the following account:
https://sub2.domain.com/dir/
Username: admin
If this was a mistake, just ignore this email and nothing will happen.
To reset your password, visit the following address:
<https://sub2.domain.com/dir/wp-login.php>';
post_async($posturl,$message_array);
function post_async($url, $params)
{
// Build POST string
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
// Connect to server
$parts=parse_url($url);
$fp = fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno, $errstr, 30);
// Build HTTP query
$out = "POST ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
$out.= $post_string;
// Send data and close the connection
fwrite($fp, $out);
fclose($fp);
}
?>
However, if in the variable $message_array['Body'], if I remove .php in the last line (That is, to replace wp-login.php with wp-login), the script works as expected.
I'm very surprised about this, since .php does not contain any special character.

Its now showing error possible because php error reporting is off on your server.
At least it shows you error on this line where you are calling the function before its declaration.
post_async($posturl,$message_array);
You should turn on errors on your server so that you can come to know what problem you are facing. Also, you have to call the function after its declaration. Just like given below...
function post_async($url, $params)
{
// Build POST string
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
post_async($posturl,$message_array);

Related

PHP fire and forget not working with remote server

I'm trying to send a request to a remote server using the fire-and-forget approach. This is my code:
function backgroundPost($url, $data = array()){
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$encoded_data = json_encode($data);
$output = "POST ".$parts['path']." HTTP/1.1\r\n";
$output .= "Host: ".$parts['host']."\r\n";
$output .= "Content-Type: application/json\r\n";
$output .= "Content-Length: " . strlen($encoded_data) . "\r\n";
$output .= "Connection: Close\r\n\r\n";
$output .= $encoded_data;
fwrite($fp, $output);
fclose($fp);
return true;
}
}
//Example of use
backgroundPost('url-here', array("foo" => "bar"));
but the data that arrives is simply empty.
When I spin up the application locally and send the request to my own machine instead, the data does arrive.
Am I misunderstanding something about this pattern?
Why is it working when sending a request to my own machine but not a remote one?
Thanks!

curl or file_get_contents ignore output

I need to run a php code in a different server (let's call it server 2) from server 1.
In server 1, I have something like this
<?php
file_get_contents('http://domain_in_server_2.com/php-script.php');
?>
The problem is, this request may take long time, and I don't need to get the output. I just want to trigger the script without having to wait or getting the output.
Is there anyway to accomplish what I want?
Thank you very much.
You can use a socket. See this example.
Edit:
Here is the code from the above link:
// Example:
// post_async("http://www.server.com/somewhere",array("foo" => 123, "bar" => "ABC"));
function post_async($url, $params)
{
// Build POST string
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
// Connect to server
$parts=parse_url($url);
$fp = fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno, $errstr, 30);
// Build HTTP query
$out = "$type ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
$out.= $post_string;
// Send data and close the connection
fwrite($fp, $out);
fclose($fp);
}

php post empty if textarea over 1 million characters

I have a textarea, it works fine if there are less than 1 million characters. As soon as something is posted over 1 million characters that field is missing in $_POST (all other fields are posted).
What could be causing this? When searching all I have seen mentioned is there are no character limits only post size limit and memory limit. No errors are being displayed and these are set well over the 2Mb text size.
Managed to solve the issue, it was suhosin limiting the characters. I needed to set the following:
suhosin.request.max_value_length = 100000000
suhosin.post.max_value_length = 100000000
Did you try increasing post_max_size in php.ini file?
Moreover, you can also use some other way to post your data i.e use:
//execute an async method
function curl_request_async($url, $params, $type='GET'){
$post_params = array();
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
(isset($parts['scheme']) && $parts['scheme'] == 'https')? 443 : 80,
$errno, $errstr, 999999999);
$out = "$type ".$parts['path'] . (isset($parts['query']) ? '?'.$parts['query'] : '') ." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
// Data goes in the request body for a POST request
if ('POST' == $type && isset($post_string)) $out.= $post_string;
fwrite($fp, $out);
fclose($fp);
}
You can call this function like:
$url = "somefile.php";
$data['mydata'] = "some data some data some data";
curl_request_async( $url, $data, 'POST' );
Note:
Do not use any type of Session based authentication in file "somefile.php". And you can access post data like $_POST['mydata']. You can also call this function in by GET. Don't know what you are actually looking for.

PayPal IPN generating a HTTP 302 error using PHP

I have an IPN script that runs, and has worked for some time now. Recently I started getting an HTTP/1.1 302 Moved Temporarily as a response and cannot determine why.
The following is the code related to posting to PayPal and getting the response:
$sd = #fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if(!$sd) {
$error = 'Error opening socket connection to PayPal: '.$errstr;
quit($error, $errno);
}
$req = 'cmd=_notify-validate';
foreach($_POST as $key=>$value) $req .= "&{$key}=".urlencode(stripslashes($value));
// post back to PayPal to validate
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($req)."\r\n";
$header .= "Host: http://www.paypal.com/\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($sd, $header.$req);
$response = '';
while(!feof($sd)) $response .= fgets($sd, 4096);
fclose($sd);
Note, all the connection, transfer, and responses work, I do not get and error. But the response from PayPal is not correct in that it does not provide VERIFIED or INVALID as stated in their documentation, but rather an HTTP 302 error.
I build my request this way (and it works). Maybe it can help you
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
//Fixes some special characters Paypal sends
$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}', $value);
$req .= '&' . $key . '=' . $value;
}
Host, in the HTTP header, must be set to www.paypal.com. Notice the lack of http[s]://.

Something like thread in php

I'm trying to program yahoo messenger robot,now my robot can get messages and answer to them.
Yahoo only can send 100 pm at each time I get notification.
now I wanna answer the each pm. I use while(true){ } to it and answer first pm,then second ,then 3rd and ... .
It's so slow ,because I can make only one connection to yahoo by this(I use curlib).
how can I send some messages at same time?I think I need something like thread but in php.
you can use pcntl_fork(). http://www.php.net/manual/en/function.pcntl-fork.php
You need pcntl extension and it only works on Unix
If you use curl function you might take a look at curl_multi_init(). http://www.php.net/manual/en/function.curl-multi-init.php
I've wrote a simple function below, which launches URL, and doesn't wait for a result, so like this you can launch many URLs on your own site, it will make your loop fast, and also no need to install any extensions to your server.
function call_url_async($url, $params, $type='POST', $timeout_in_seconds = 1)
{
//call the URL and don't wait for the result - useful to do time-consuming tasks in background
foreach ($params as $key => &$val)
{
if (is_array($val))
$val = implode(',', $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, $timeout_in_seconds);
//if ($fp == FALSE)
// echo "Couldn't open a socket to ".$url." (".$errstr.")<br><br>";
// Data goes in the path for a GET request
if ('GET' == $type)
$parts['path'] .= '?'.$post_string;
$out = "$type ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
// Data goes in the request body for a POST request
if ('POST' == $type && isset($post_string))
$out.= $post_string;
fwrite($fp, $out);
fclose($fp);
}

Categories