how to construct an https POST request with drupal_http_request? - https

I want to send a POST request to an https server.
$data = 'name=value&name1=value1';
$options = array(
'method' => 'POST',
'data' => $data,
'timeout' => 15,
'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
);
$result = drupal_http_request('http://somewhere.com', $options);
I can't figure out out to implement the https options in the POST example code above.
Can anyone please explain me how to do this? I am quite new to PHP coding with Drupal, and I could definitely use the guidance.
I found that all is needed is to set it in the protocol. So I got to this code.
$data = 'access_token=455754hhnaI&href=fb&template=You have people waiting to play with you, play now!';
$options = array(
'method' => 'POST',
'data' => $data,
'timeout' => 15,
'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
);
$result = drupal_http_request('https://graph.facebook.com/1000721/notifications?', $options);
It still doesn't work. If I post via Firefox with https://graph.facebook.com/1000080521/notifications?access_token=45575FpHfhnaI&href=fb&template=You have people waiting to play with you, play now! it works.
I am probably not constructing the request properly in Drupal.
What am I doing wrong? How can I get my code to work?

There is no difference between using drupal_http_request() with a secure connection (https://), or without a secure connection (http://).
PHP must be compiled with support for OpenSSL; otherwise, drupal_http_request() doesn't support secure connections. Apart that, the only issue could be the proxy server not supporting a secure connection.
As side note, you are using https://graph.facebook.com/1000721/notifications? as URL for the request. The question mark should not be part of the URL.
I would also use drupal_http_build_query() to build the data to use for the request.
$data = array(
'access_token' => '455754hhnaI',
'href' => 'fb',
'template' => 'You have people waiting to play with you, play now!'
);
$options = array(
'method' => 'POST',
'data' => drupal_http_build_query($data),
'timeout' => 15,
'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
);
$result = drupal_http_request('https://graph.facebook.com/1000721/notifications', $options);

Related

Guzzle Post Null/Empty Values in Laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.
For example:
$response = $client->request('POST',
'https://www.testsite.com/coep/public/api/donations', [
'form_params' => [
'client' => [
'web_id' => NULL,
'name' => 'Test Name',
'address1' => '123 E 45th Avenue',
'address2' => 'Ste. 1',
'city' => 'Nowhere',
'state' => 'CO',
'zip' => '80002'
],
'contact' => [],
'donation' => [
'status_id' => 1,
'amount' => $donation->amount,
'balance' => $donation->balance,
'date' => $donation->date,
'group_id' => $group->id,
],
]
]);
After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?
At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!
I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.
The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.
I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).
Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART
Try to define anything like form_params, headers or base_uri before creating a client, so:
// set options, data, params BEFORE...
$settings = [
'base_uri' => 'api.test',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'ajustneedatokenheredontworry'
],
'form_params' => [
'cash' => $request->cash,
'reason' => 'I have a good soul'
]
];
// ...THEN create your client,
$client = new GuzzleClient($settings);
// ...and finally check your response.
$response = $client->request('POST', 'donations');
If you check $request->all() at the controller function that you are calling, you should see that were sent successfully.
For those using laravel, use this:
Http::asJson()

CakePHP modal AJAX checks not displaying in lights out box?

Ok, this might have a very simple answer but for the life of me my I can not seem to find an answer! I am signing up users from a text into a lights out box (SimpleModal) which AJAX loads a new page for an admin to sign up a user to a selected client list.
This all works without any issues at all, so long as the model checks are correct. I have two checks, one makes sure the username is unique and that the password has at lest 8 characters, code below. But when one or both of these checks are not meet, then the user is taken to the AJAX URL and the 'message' is then displayed. This is not what I need I need it to be taken back to the lights out box or set these messages as flash error message to be printed on to the screen.
Or should I remove these checks from the modal and get JQuery to check them instead?
Any ideas?
All help very welcome.....
Model Code ::
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('isUnique'),
'message' => 'Sorry but a unique username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('minLength', '8'),
'message' => 'Sorry but a password of 8 characters or more is required'
)
) ... more check follow but these are the issues....
CTP file ::
$this->layout = 'ajax';
$AddUserForm = $this->Form->create('User', array('url' => '/ADD-USER-URL-HERE'));
$AddUserForm .= $this->Form->input('username');
$AddUserForm .= $this->Form->input('password');
$AddUserForm .= $this->Form->input('role', array('options' => array('admin' => 'Admin', 'user' => 'User')));
$AddUserForm .= $this->Form->input('data_id', array('options' => array($data), 'empty'=>true));
$AddUserForm .= $this->Form->end(__('SAVE NEW USER'));
echo $AddUserForm;
In your ctp file
$('form').on('submit', function(e) {
e.preventDefault();
$.post($(this).attr('action'), $(this).serialize(), function(res) {
$('form').replaceWith(res);
})
})
can be used for validating the data using the above script,
I think this will help you for submitting the form via ajax and you can also display the error messages using the response coming from the ajax.
Hope this will help you.

receive more response data in ci-merchant library codeigniter

How can I receive more response data in the ci-merchant codeigniter library ?
I am using the Paypal Express checkout payment method.
And I am passing the following parameters:
$params = array(
'amount' => 100.00,
'currency' => 'USD',
'return_url' => my return url,
'cancel_url' => my cancel url );
Right now am getting just the following response
Merchant_paypal_api_response Object
(
[_status:protected] => complete
[_message:protected] =>
[_reference:protected] => 1K088384XU0947545
[_data:protected] =>
[_redirect_url:protected] =>
[_redirect_method:protected] => GET
[_redirect_message:protected] =>
[_redirect_data:protected] =>
)
How can I get the data like paypal id, shipping address, item name and other stuff that paypal returns in the DoExpressCheckoutPayment response ?
Actually, that info wouldn't come back in the DECP response. It would come back in GetExpressCheckoutDetails.
Your library should provide some way to see the RAW API requests and responses. If it's not parsing out all of the details for you you'll need to do that on your own.
This isn't exactly an answer to your question, but you should try using Omnipay instead. Omnipay is basically CI-Merchant V2 (I'm the author of both libraries).
Omnipay lets you have direct access to the raw response. E.g. you would do something like this:
$params = array( 'amount' => 1000, 'currency' => 'USD', 'returnUrl' => 'my return url', 'cancelUrl' => 'my cancel url' );
$response = $gateway->completePurchase($params)->send();
$reference = $response->getTransactionReference(); // paypal transaction id
$data = $response->getData(); // this is the raw response object

Magento REST API Updating

I have been trying to update a field on a customer information via the REST API using this link format:
http://<magentohost>/api/rest/customers
But to I am getting a customer email already exist
How can I update information via REST API..
Sample Code:
$productData = json_encode(array(
'id' => 1,
'firstname' => 'Ted',
'lastname' => 'Mosbius',
'website_id'=> 1,
'group_id' => 1,
'email' => 'ted#mosbis.com'
));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
// $oauthClient->fetch($resourceUrl);
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
In this code, email exist, id exist , website_id exist and group_id exist... I just wanted to update the firstName and lastName
Thanks in advance
Just found the problem, I need to use PUT instead of POST when updating based from this link (http://ajaxpatterns.org/RESTful_Service)...
$productData = json_encode(array(
'id' => 1,
'firstname' => 'Ted',
'lastname' => 'Mosbius',
));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_PUT, $headers);
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
Hopefully this will help someone in the future... :)
Errors do come up less the header is formed like this:
$headers = array('Content-Type' => 'application/json', 'Accept' => '*/*');

CodeIgniter, CI-Merchant and Paypal Sandbox

I'm trying to make a little shopping cart with CodeIgniter and I found CI-Merchant to work with payment gateways with this guide http://ci-merchant.org/ but I don't really understand how to make it works with Paypal Sandbox.
$this->load->library('merchant');
$this->merchant->load('paypal_express');
$settings = array(
'username' => 'test#test.com',
'password' => '********',
'signature' => 'Test Store',
'test_mode' => true);
$this->merchant->initialize($settings);
$params = array(
'amount' => 12.00,
'currency' => 'CAD',
'return_url' => 'http://payment.test.com',
'cancel_url' => 'http://payment.test.com/cancel');
$response = $this->merchant->purchase($params);
$this->load->view('welcome_message');
I know that this code can't do much but it do nothing at all. Just load the view and nothing happens, I don't understand. So, my question is, do you know tutorials or just how to make CI Merchant works with Paypal Sandbox? Thanks for the help.
Ace's comment is spot on. There is nothing wrong with your code, but you need to inspect the $response object to see what the result (or error message) was.
$response = $this->merchant->purchase($params);
if ($response->success())
{
// mark order as complete
$gateway_reference = $response->reference();
}
else
{
$message = $response->message();
echo('Error processing payment: ' . $message);
exit;
}
You can also simply try this to inspect the object:
$response = $this->merchant->purchase($params);
echo '<pre>';
print_r($response);
exit;

Resources