How to use API in wordpress - php

I would like to connect one Razorpay Payment API with WordPress, API has authentication token using username and password.
is there any builtin functionality available in WordPress to make a call and handle response?

you can use wp_remote_get()
for example
wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) );
you can also control all request parameters like headers and body data.
Default Usage
global $wp_version;
$args = array(
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => true,
'stream' => false,
'filename' => null
);
Reference : More info

Related

Active Campaign API 403 Error using wp_remote_post();

Im creating a simple integration that should post our WooCommerce orders over to the Deep Data seciton via the API (V3)
Here is a simple example of the request Im trying to make.
Im running this script manually for the time being just to get it working. This is the array Im sending as my request using wp_remote_post($url, $request)
Array
(
[key] => KEY
[url] => URL/ecomOrders
[settings] => Array
(
[method] => POST
[timeout] => 5
[redirection] => 5
[httpversion] => 1.0
[user-agent] => WordPress/5.2.1; https://www.XXXX.com
[blocking] => 1
[body] => {"ecomOrder":{JSONORDER}}
[headers] => Array
(
[Api-Token] => KEY
)
)
)
This is (part of) what I get back from my response.
[body] =>
[response] => Array
(
[code] => 403
[message] => Forbidden
)
I have double checked the API key and URL and just a side note, we are already using the same method and script details in a similar reques to add contacts which is working fine.
Here is the code Im using (all $var's are defined earlier in the script):
$request = array(
'key' => $key,
'url' => $url,
'settings' => array(
'method' => 'POST',
'sslverify' => false,
'timeout' => 5,
'redirection' => 0,
'httpversion' => '1.0',
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
'body' => $body,
'headers' => array(
'Api-Token' => $key,
)
)
);
$response = wp_remote_post($url, $request);
We just ran into a similar issue today; where all the headers and payload were set correctly, but the API was returning a 401.
Our payload needed to be sent as json and we had to explicitly define that in the headers. Like so:
'content-type' => 'application/json'
Also, it look likes the request/args array isn't structured how WordPress recommends in the codex. (arguements)
$key = 'myKey';
$url = 'myURL'
$body = array('ecomOrder' => $myOrder);
$request = array(
'method' => 'POST',
'sslverify' => false,
'timeout' => 5,
'redirection' => 0,
'httpversion' => '1.0',
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
'body' => json_encode($body),
'headers' => array(
'content-type' => 'application/json',
'Api-Token' => $key,
)
);
$response = wp_remote_post($url, $request);
This may be a shot in the dark as I'm not familiar with Active Campaign's API, but hopefully it helps.
Resources
This stackoverflow article really helped.

How to use Wordpress wp_remote_post if I don't need to get a response?

Using wp_remote_post to send form data (Contact Form 7) to external API (CRM). API is heavy (checking emails, confirmation letters, etc), so I don't want PHP to block any processes, while waiting for a response (I don't need response at all, just send).
Still, even with 'blocking' => false it does — if I activate confirmation emails on external API, Wordpress users need to wait several seconds before form is processed.
What am I doing wrong? :) Code:
// POST-request to API
wp_remote_post('http://crm.site.com/get_record', array(
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => false,
'headers' => array() ,
'body' => $send_data,
'cookies' => array()
));
I think method is missing here, add method and try
$response = wp_remote_post('http://crm.site.com/get_record', array(
'method' => 'POST',
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => false,
'headers' => array() ,
'body' => $send_data,
'cookies' => array()
));
And check response:
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>';
}

Dompdf package for Laravel 4 return 500 when many ( >9 ) pages are generated

I'm using this Dompdf package for Laravel 4 by Jonathan Thuau
and I'm getting server error whenever I try to generate more than 9 gapes otherwise it works.
Below is the screenshot of the error
Below is also my config file , let me know if I have to change anything in that
return array(
'DOMPDF_TEMP_DIR' => sys_get_temp_dir(),
'DOMPDF_UNICODE_ENABLED' => true,
'DOMPDF_PDF_BACKEND' => 'CPDF',
'DOMPDF_DEFAULT_MEDIA_TYPE' => 'screen',
'DOMPDF_DEFAULT_PAPER_SIZE' => 'letter',
'DOMPDF_DEFAULT_FONT' => 'serif',
'DOMPDF_DPI' => 96,
'DOMPDF_ENABLE_PHP' => false,
'DOMPDF_ENABLE_REMOTE' => false,
'DOMPDF_ENABLE_CSS_FLOAT' => false,
'DOMPDF_ENABLE_JAVASCRIPT' => true,
'DEBUGPNG' => false,
'DEBUGKEEPTEMP' => false,
'DEBUGCSS' => false,
'DEBUG_LAYOUT' => false,
'DEBUG_LAYOUT_LINES' => true,
'DEBUG_LAYOUT_BLOCKS' => true,
'DEBUG_LAYOUT_INLINE' => true,
'DOMPDF_FONT_HEIGHT_RATIO' => 1.1,
'DEBUG_LAYOUT_PADDINGBOX' => true,
'DOMPDF_ENABLE_HTML5PARSER' => false,
'DOMPDF_ENABLE_FONTSUBSETTING' => false,
'DOMPDF_ADMIN_USERNAME' => 'user',
'DOMPDF_ADMIN_PASSWORD' => 'password',
);
The error was due to resource exhaustion and I have to add to following lines on top of the page and it worked
set_time_limit(0);
ini_set("memory_limit",-1);
ini_set('max_execution_time', 0);

Bad characters when generating pdf file with Knp Snappy

I am using Symfony2. When the pdf file is generated using this code :
public function printAction($id)
{
// initialiser $demande
$html = $this->renderView('PFETimeBundle:Demande:print.html.twig',
array('demande'=> $demande)
);
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
}
I get this content (french characters appear in bad characters) :
try to add the encoding property
'encoding' => 'utf-8',
heres a full copy of my working code, pls note that i pass an options array as second argument to getOutPutFromHtml()
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html, array(
'orientation' => 'landscape',
'enable-javascript' => true,
'javascript-delay' => 1000,
'no-stop-slow-scripts' => true,
'no-background' => false,
'lowquality' => false,
'encoding' => 'utf-8',
'images' => true,
'cookie' => array(),
'dpi' => 300,
'image-dpi' => 300,
'enable-external-links' => true,
'enable-internal-links' => true
)),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="report.pdf"'
)
);
If you are using the generateFromHtml method, you have to use it like this, at third parameter:
$this->container->get('knp_snappy.pdf')->generateFromHtml(
$this->container->get('templating')->render(
'YourBundle:Template:pdfTemplate.html.twig',
array(
'var' => $var,
)
),
'/path/to/file.pdf',
array(
'encoding' => 'utf-8',
)
);

Wordpress HTTP API results in JSON_ERROR_SYNTAX

I'm Trying to correctly use wp_remote_get in my wordpress plugin which uses our API. The API checks the request body for is_json, which returns 4 - aka JSON_ERROR_SYNTAX. Using cURL has the desired response. Here's my code:
$args = array(
'body' => array('api_key' => 123),
'timeout' => '5',
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'cookies' => array()
);
$result = wp_remote_request('https://myapi.endpoint/api/1.0/request', $args);
var_dump($result['body'])
var_dump: string(13) ""api_key=123""
whereas the var_dump of my cURL request is string(15) "{"api_key=123"}"
Any ideas as to what I am doing incorrectly?
As Marc B stated, wp isn't sending json. You need to set the content type in the headers, and also send it as json:
$body = array('api_key' => 123);
$headers = array (
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Content-Length' => strlen(json_encode($body)
);
$args = array(
'method' => 'POST',
'headers' => $headers,
'body' => json_encode($body)
);

Categories