I want to send a form to another website using php - php

I've been trying to send a form my website to another website using this code
$jumlah = $query1->jumlah;
$postdata = http_build_query(
array(
'url' => $url,
'jumlah' => $jumlah
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://insta.kingdompanel.xyz/addlikes.php', false, $context);
Before I send it to that website, I'm trying to send the form to my own website, and it works. I change this code to try send a form there.
file_get_contents('http://insta.kingdompanel.xyz/addlikes.php', false, $context);
After I change that, it doesn't send anything.
update :
here is it the network tab
my goal is to post this data
to this form which is different website, without opening this page

In the request header you can add 'header' => 'Access-Control-Allow-Origin: *' and try. It should work. Even after that request fails means, other server may be blocking it

$jumlah = $query1->jumlah;
$postdata = http_build_query(
array(
'url' => $url,
'jumlah' => $jumlah
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'header' => 'Access-Control-Allow-Origin: *',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://insta.kingdompanel.xyz/addlikes.php', false, $context);
Try the above modified code, if it works fine otherwise, Other server may not allowing CORS requests

Related

Sent post data by file_get_contents, post data readable by the client/browser?

if I use file_get_contents for sending post data to another .php with like the example given by the manual further down, could a visitor of this webpage have any possiblity to view/trace the post data that i send? Thanks
<?php
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
?>
https://www.php.net/manual/en/context.http.php

Error in getting Active subscriber count from Sendy API

While trying to fetch the active subscriber count from Sendy API, I am getting following error
The POST request looks like this:
http://my-sandy-Installation/api/subscribers/active-subscriber-count.php
In header: Content-Type:application/x-www-form-urlencoded
In Body:
api_key= mykey
list_id= mylistid
Can anyone please help?
$boolean = 'true';
$postdata = http_build_query(
array(
'email' => $email,
'api_key'=>'your api_key',
'list' => 'Your List id',
'boolean' => 'true'
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($your_installation_url.'/subscribe', false, $context);

Automated login to Stackoverflow

I get quite annoyed by logging in to SO sometimes and i'd like to do it by just calling a PHP file. I tried doing so by sending a post request like this:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f';
$data = array('email' => 'mymail#gmail.com', 'password' => 'mypasswort');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
I also tried including more values like ssrc and fkey but i just shows the SO mainpage after submitting. It even stays at localhost (or wherever the script is running). If i enter a wrong password it marks it as incorrect - so it has to work at lest in some way (the data verification)...
You must send post data:
isSignup:false
isLogin:true
isPassword:false
isAddLogin:false
hasCaptcha:false
fkey:922b6dc5a5a375283c44e298246d7763
ssrc:head
email:asd
password:
submitbutton:Log in
oauthversion:
oauthserver:
openidusername:
openididentifier:
You should send first GET request and parse fkey from result page (this is CSRF protection).
fkey you should send with POST request with post data which I show you.
Simple example:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head';
$data = array(
'email' => 'mail#email.z',
'password' => 'pass',
'isSignup' => false,
'isLogin' => true,
'isPassword' => false,
'isAddLogin' => false,
'hasCaptcha' => false,
'fkey' => '',
'ssrc' => 'head',
'submitbutton' => 'Log in',
'oauthversion' => '',
'oauthserver' => '',
'openidusername' => '',
'openididentifier' => ''
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
preg_match('~name\=\"fkey\"\svalue=\"([\w]+)\"~', $result, $matches);
$data['fkey'] = $matches[1];
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
But I recommend for you use SO API and you willn't have any problems
If you will have problem, write

file_get_contents: get full response even on error

Is it possible to make file_get_contents show me the actual response even if an error occurs?
It is difficult to debug otherwise. For instance, say you have the following code:
$url = 'https://api.twitter.com/oauth/request_token';
$data = array();
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = #file_get_contents($url, false, $context);
var_dump($result);
var_dump($http_response_header);
This shows me NULL for the result and the HTTP headers, but I want to get the actual message Twitter sends back (should be something like Failed to validate oauth signature and token), which is what you get if you try to load https://api.twitter.com/oauth/request_token in your browser.
There is a very simple switch for the context. just add this line to options:
'ignore_errors' => true
so you will get
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
'ignore_errors' => true
)
);

How to get HTTP response headers after POST request in PHP?

I want to know if it's possible to read/parse the HTTP response header after a POST request in PHP without the use of cURL..
I have PHP 5 under IIS7
The code I use to POST is :-
$url="http://www.google.com/accounts/ClientLogin";
$postdata = http_build_query(
array(
'accountType' => 'GOOGLE',
'Email' => 'xxxxx#gmail.com',
'Passwd' => 'xxxxxx',
'service' => 'fusiontables',
'source' => 'fusiontables query'
)
);
$opts = array('http' =>
array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
Above, im doing a simple ClientLogin Authentication to google and I want to get the Auth token which returns in the header. Echo-ing $result only gives the body content and not headers which contains the auth token data.
The function get_headers() may be the one you are looking for.
http://php.net/manual/en/function.get-headers.php
Use the ignore_errors context option (documentation):
$opts = array('http' =>
array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $postdata,
'ignore_errors' => true,
)
);
Also, maybe use fopen rather than file_get_contents. You can then call stream_get_meta_data($fp) to get the headers, see Example #2 on the above link.

Categories