Handle incoming Array from Guzzle - wordpress

So i have been trying to get guzzle working, i can send a post request which sends some kind of array and I would like to know, how can i receive it at an endpoint? i can handle things like if(isset($_POST['nameHere'])) but not the array thingy
$client = new Client();
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'email' => 'test#gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
],
'debug' => true
]);
echo '<pre>' . print_r((string)$response->getBody(), true) . '</pre>';
}
ps: My custom laravel application is the sender, my wordpress site is the receiver.

Got it working, i was missing the .php ending in my url, because i chose not to test with httpbin but directly with my api.
and in wordpress i dont get anything because this is the sendercode i published not the receiver (wordpress). i wouldnt know how to test it. thanks anyway

Related

How to connect WordPress / WooCommerce to a REST API with PHP?

I am trying to write my first REST API with PHP. We want to send shipping data to the shipping company and get labels for printing in return. The shipping company has given me test api credentials.
So I started to write a plugin with this code inside:
$url = "https://mywebsite/endpoint/";
$username = "myusername";
$password = "mypassword";
$data = array(
'key1' => 'value1',
'key2' => 'value2'
);
$response = wp_remote_post( $url, array(
'body' => $data,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
)
);
var_dump($response); // not being called
When I run this code, I get a white screen with no error message, the last line with var_dump($response) is not being called. I am a bit stuck here because I se no success and no error...what could it be? Why this behaviour of the remote server? Does it mean "sorry, wrong credentials" or "data has the wrong format"? I have the feeling that the server doesn't even notice that I'm trying to contact him...
I tried a number of other variatons of the above code that I found somwehere online, and also outside of WordPress, but no success.
The shipping company gave me a documentation. It says:
HTTP Method: POST
Authentication Header: Basic Authentication (user name/ password)
so I thought I could do no wrong when trying that. Hm.
I found a solution, so I can share it here with you.
I installed the free Software Postman and created a simple POST query there. After entering $url, $username and $password I ran a query and it worked well. the good thing abort Postman is that it creates code snippets for you. "very_long_cryptic_string" is obviously created from $username and $password. The PHP for my case goes like this, and it works:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic very_long_cryptic_string'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

How to send data back to woocommerce custom gateway after processing

I am trying to get response data back to my payment gateway with no success.
This is the code snippet within my woocommerce extension that sends data to my payment processor(index.php)
$environment_url = "https://example.com/api/index.php";
$payload = array(
// Credentials and API Info
"x_tran_key" => $this->trans_key,
"x_login" => $this->api_login,
"x_version" => "3.1",
// Order total
"x_amount" => $customer_order->order_total
);
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
I get the data very fine to the url(index.php). The issue is getting the response back to my custom woocommerce extension so that I can complete payment_process()
Most resources I have found online only talk about sending data, but not how the response or result is sent back to the querying resource file.
Any help will be highly appreciated. Thank you.
Just did a simple print_r and it did the trick, I got my results in an array and used print_r
If anybody encounters the same problem as I did, I hope this has saved your time.

how to create uninstall webhook in shopify using laravel wrapper?

i am trying to create webhook using the api call inlaravel but it gives me the error "The requested URL returned error: 406 Not Acceptable".
my code is as bellow.
$webhookData = [
'webhook' => [
'topic' => 'app/uninstalled',
'address' => url('/uninstall'),
'fields' => 'name',
'format' => 'json'
]
];
$uninstall = $sh->call(['URL' => 'https://' . $shop . '/admin/webhooks.json', 'METHOD' => 'POST', 'DATA' => $webhookData]);
There are certain things you need to take care of while creating the Shopify webhooks.
The address field should be a globally accessible URL (ex: https://example.com/webhook/uninstall.php)
You can not give localhost links for creating webhooks.
I think the address field is not passed with a valid URL in your post request.

Facebook Posting using FB API phpSDK

i created my app on fb with permissions ready to publish on users behalf, the thing is, a regular post has Like and Comment links, like buttons on bottom of the post, i want to add my custom link : VOTE NOW, its a poll post
how can i do that?
someone gave an answer but for js sdk not php, n i cant find it on facebook dev documentation
some gave a close enough solution, but ddnt seem to work on php with modifications
FB.ui({
method: "feed",
link: "LINK_URL",
...
actions: [
{ name: "Read Now", link: "URL TO THE READ NOW " }
]
}, function(response) { console.log(response); });
It seems it's working with /me/feed but with my custom /me/xxxxxx:submitted_a_poll/ its not working
When you make a call with the PHP SDK, you also pass a similar set of parameters:
$attachment = array(
'link' => 'http://your-cool-site.com',
'description' => 'This is the description',
...
'actions' => array(
array(
'name' => 'Vote Now!',
'link' => 'http://your-cool-site.com/vote.php'
)
)
);
$result = $facebook->api('/me/feed/', 'post', $attachment);
All you really have to do is add the relevant action settings to your parameters.

Edit mail.inc to enable SMTP server for sending mail

i want to place the following codes into include/mail.inc of Drupal7 so that i can send mail from SourceForge's project web space. Don't ask me to install SMTP Authentication Support, and i don't have access to php.ini , I wonder where should these codes be placed? Thanks in advance!
include('Mail.php');
$recipients = array( 'someone#example.com' ); # Can be one or more emails
$headers = array (
'From' => 'someone#example.com',
'To' => join(', ', $recipients),
'Subject' => 'Testing email from project web',
);
$body = "This was sent via php from project web!\n";
$mail_object =& Mail::factory('smtp',
array(
'host' => 'prwebmail',
'auth' => true,
'username' => 'YOUR_PROJECT_NAME',
'password' => 'PASSWORD', # As set on your project's config page
#'debug' => true, # uncomment to enable debugging
));
$mail_object->send($recipients, $headers, $body);
if this code could success send email without drupal7 (you must make sure it)
then you could do it in three ways:
write a drupal 7 module ,copy mail.php into *.module, and make the rest code as a function,that's the way as the handbook of drupal.
just copy all of the code to you theme/page.tpl.php , and run it directly , a little dirty
hack drupal core , include/mail.inc , just change function drupal_mail_send

Resources