wp_remote_get( $url, $args ) not working? - php

I'm trying to request data from the virgin giving api to display my fundraising information on my blog. I'm a complete novice and have been unable to workout how such a task would be completed after reading and trying out everything I can find on the topic. This is the code I have been using in a .php file within a wordpress installation on cloud9 (I have removed the search and api key). I have also tried the code on a page on my live blog which is hosted with Bluehost. I'm not sure if I'm creating the page in the correct location or if the below code should be wrapped in some sort of function. Any help that you can provide would be much appreciated.
$url = "https://api.virginmoneygiving.com/fundraisers/v1/";
$args = array(
'headers' => array( "Content-type" => "application/json" ),
'httpversion' => 1.1,
);
$response = wp_remote_get( $url, $args );
$body = wp_remote_retrieve_body( $response );
$response_code = wp_remote_retrieve_response_code( $response );
print_r( $response_code );
echo $response;

'httpversion' takes a string...
'httpversion' => '1.1',
Read more in the source.

Related

PrestaShop 1.5 adding PHP (reCaptcha)

currently I'm working on an outdated PrestaShop instance in version 1.5. There is a requirement to integrate Google's reCaptcha. So far that was easy. Except the Respond code, which is a PHP script, does not work yet.
Is there an easy way to integrate a PHP code into such a PrestaShop? It has to be loaded before the HTML code.
The main issue is, that I have no idea how to implement a PHP-Code in a PrestaShop 1.5. There aren't any tutorials or documentations anymore. And most hints are for codes inside CMS-pages, but I need it above the head.
The code I want to implement looks like that:
<?php
if(isset($_POST['submitMessage'])){
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = [
'secret' => "***",
'response' => $_POST['token']
];
$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);
$response = file_get_contents($url, false, $context);
echo "<pre>";
print_r($response);
}
?>
I'd be grateful for any help.

WordPress notification rest API code

Like WordPress give us rest API for comments, posts etc. I want to get notification API code.
developer.wordpress.com gives the documentation for how to use notification API.
https://developer.wordpress.com/docs/api/1/get/notifications/$note_ID/.
To use this API, I need WordPress rest API notification code.
How can I get that code.
WordPress rest API code doesn't have notification code.
https://wordpress.org/plugins/rest-api/
Please help me.
You can use following code to get notification id using rest api.
Note : assume id is 1801481297
<?php
$options = array (
'http' =>
array (
'ignore_errors' => true,
'header' =>
array (
0 => 'authorization: Bearer YOUR_API_TOKEN',
),
),
);
$context = stream_context_create( $options );
$response = file_get_contents(
'https://public-api.wordpress.com/rest/v1/notifications/1801481297',
false,
$context
);
$response = json_decode( $response );
?>

How to Integrate 3rd party API in Wordpress

I'm using wordpress and i want to integrate an SMS API into my wordpress site. Can anyone help in knowing where (in which file) to write the code for integration and also the code to integrate SMS API.
My SMS API Url is :
http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >
I want to integrate above API in my wordpress theme so that i can send sms based on mobile number and add required message.
In wordpress you can use wp_remote_get and wp_remote_post
get request example
$url = 'http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >';
$response = wp_remote_get( $url );
if( is_array($response) ) {
$header = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
}
post request example
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}

Woocommerce custom payment gateway redirect

I have a problem. I should do a custom gateway. I know the basics. I read the documentation. URL data to be transmitted (such as user name, the amount of the transaction). My question is how to redirect the user to the payment page of the bank? What is the command and where to give the exact url? And then the returned data must be processed what method? cURL or something else? I could not find any real solution to the problem.
Different gateway's have different needs, if your gateway uses a POST, you can use this to POST the data, and get a response.. it is better than cURL.
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
// Retrieve the body's response if no errors found
$response_body = wp_remote_retrieve_body( $response );
$response_headers = wp_remote_retrieve_headers( $response );
// Payload would look something like this.
$payload = array(
"amount" => $order.get_total(),
"reference" => $order->get_order_number(),
"orderid" => $order->id,
"return_url" => $this->get_return_url($order) //return to thank you page.
);
//use this if you need to redirect the user to the payment page of the bank.
$querystring = http_build_query( $payload );
return array(
'result' => 'success',
'redirect' => $environment_url . '?' . $querystring,
);

google url shortener api with wp_remote_post

I try to make google url shortener with wp_remote_post()
but I got error result,
I know how to use CURL, but CURL not allowed in WordPress!
This resource for API with WordPress:
http://codex.wordpress.org/Function_Reference/wp_remote_post
http://codex.wordpress.org/Function_Reference/wp_remote_retrieve_body
http://codex.wordpress.org/HTTP_API#Other_Arguments
http://codex.wordpress.org/Function_Reference/wp_remote_post#Related
This google url shortener API docs:
https://developers.google.com/url-shortener/v1/getting_started#shorten
This is my code:
function google_url_shrt{
$url = 'http://example-long-url.com/example-long-url'; // long url to short it
$args = array(
"headers" => array( "Content-type:application/json" ),
"body" => array( "longUrl" => $url )
);
$short = wp_remote_post("https://www.googleapis.com/urlshortener/v1/url", $args);
$retrieve = wp_remote_retrieve_body( $short );
$response = json_decode($retrieve, true);
echo '<pre>';
print_r($response);
echo '</pre>';
}
The WordPress API requires that the headers array contain an element content-type if you want to change the content type of a POST request.
Also, it looks like the body of your HTTP request is being passed as a PHP array, not as a JSON string as the Google Shortener API requires.
Wrap the array definition for body in a json_encode statement, and make the headers field a sub-array, and give it a shot:
$args = array(
'headers' => array('content-type' => 'application/json'),
'body' => json_encode(array('longUrl' => $url)),
);
Alternative, you could just write the JSON format yourself as it is fairly simple:
$args = array(
'headers' => array('content-type' => 'application/json'),
'body' => '{"longUrl":"' . $url . '"}',
);

Categories