POST request only with http_build_query in php - php

I need to create a POST request with http_build_query. Following are my codes:
$uri_args = array (
'name' => 'Jack',
'surname' => 'Jackson',
'username' => 'jackson12',
'email' => 'Jack#mail.com',
);
$uri = http_build_query($uri_args);
header("Location: http://samplesite.com?$uri");
At the moment it generates a request like GET, but I need POST.
Consider that I do not want to use curl, ... only http_build_query.

<?php
$data = array(
'name' => 'Jack',
'surname' => 'Jackson',
'username' => 'jackson12',
'email' => 'Jack#mail.com',
);
$query = http_build_query($data);
// create context
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,
'content' => $query,
),
));
// send request and collect data
$response = file_get_contents(
$target = "http://example.com/target.php",
$use_include_path = false,
$context);
//some actions with $response
//...
// redirect
header("Location: http://samplesite.com");

Related

PHP Retrieve the headers of a response to a POST request a key-value pair

I have this php scripts:
$url = "https://mysite/web/session/authenticate";
$data = array (
'params' =>
array (
'db' => 'mydb',
'login' => 'myuserid',
'password' => 'myuserpwd',
),
);
$data = json_encode($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/json\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'method' => 'POST',
'content' => $data,
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
From the server response I need to retrieve a key-value pair (set-cookie) contained in the headers.
Response fron POSTMAN
How can I do?
Thanks

how to send post to a class form a url?

I want to write a simple PHP code that will send data to a class in a URL.
I've written code, but it's only for sending data directly to the URL.
How to write this code?
The class name is 'sendmsg'.
<?php
$url = 'https://thxmain.cellmobilen.net:443/Charg/service/ChargingMobile';
$data = array(
'username' => '?',
'password' => '?',
'domain' => '?',
'channel' => '?',
'mobilenum' => '?',
'serviceId' => '?',
'price' => '?',
'trackingId' => '?'
);
$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);
if ($result !== FALSE) {
var_dump($result);
}else{
print( "Code is wrong" );
}
?>

Codename one push notifications with php issue

I've enabled push notifications in my app, added the build hints, registered the api on the play developer console, created and loaded the apple certificates on my server. When I test the app on a device it successfully registers for push notifications. However my issue comes in with trying to actually send a push notification. I want it to send through PHP. I use this code which is taken straight from the developer guide. However this does not work... Is it a problem with my code or did I do something wrong in the enabling push notifications process.
<?php
include("config.php");
$args = http_build_query(array( 'certPassword' => 'XXXXXXXX', 'cert'
=>
'http://kyven.co.za/mibrand/certificate/XXXX.p12',
'production' => false,
'device' => null, 'packageName' => 'za.co.bonyelo.mibrand', 'email'
=>
'kyri88#gmail.com', 'type' => 1,
'auth' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
'body' => 'Test'));
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content'
=> $args
) );
$context = stream_context_create($opts);
$response = file_get_contents("https://codename-
one.appspot.com/sendPushMessage", false, $context);
die(json_encode($response));
?>
Got it. This is the code I used
<?php
include("config.php");
$args = http_build_query(array('token' => 'XXXXXXXXXXXXXXXXXXX',
'certPassword' => 'XXXXXXXX', 'cert' =>
'http://XXXXXXX/XXXXX/XXXXX/Certificates.p12',
'production' => false,
'device' => 'cn1-ios-XXXXXXXXXXXXXXXXXXXXXXXX',
'packageName' => 'za.co.bonyelo.mibrand', 'email' =>
'kyri88#gmail.com', 'type' => 1,
'auth' => 'XXXXXXXXXXX',
'body' => 'EAT MY BALLS'));
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $args
) );
$context = stream_context_create($opts);
$response =
file_get_contents("https://push.codenameone.com/push/push", false,
$context);
die(json_encode($response));
?>

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

drupal_http_request send Your client has issued a malformed or illegal request when spaces

drupal_http_request send "Your client has issued a malformed or illegal request" when spaces in url:
$uri = "https://www.googleapis.com/freebase/v1/search?query=&prefixed=true&limit=10&output=(type )&lang=en";
$options = array(
'method' => 'GET',
'timeout' => 3,
'headers' => array(
'Accept' => 'application/json',
),
);
$result = drupal_http_request($uri, $options);
" &output=(type ) " there is space after type . if i remove space then its working fine. but space give 400 bad request error. why?
The 'proper' Drupal way:
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => $search_string,
'output' => '(description owner)',
'filter' => '(any type:product type:brand)',
'limit' => YILD_FREEBASE_MAX_HITS,
'prefixed' => 'true',
'lang' => $lang ,
);
$uri = url($serivce_url, array('query' => $params, 'external' => TRUE));
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => $search_string,
'output' => '(description owner)',
'filter' => '(any type:product type:brand)',
'limit' => YILD_FREEBASE_MAX_HITS,
'prefixed' => 'true',
'lang' => $lang ,
);
$uri = $service_url . '?' . http_build_query($params);
This solve my problem .

Categories