I’m trying to integrate a 3rd party API with WordPress. I fear this is above my head. I was this code but I’m not exactly sure how to make is work in WordPress. Is it possible?

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://xxx');
curl_setopt($ch, CURLOPT_POST, 7);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
    'auth_token' => 'xxxxxx',
    'list_id' => 'xxxxx,
    'name' => 'Office',
    'campaign_id' => 'xxxxx',
)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);

1 Answer
1

Something like this works:

$url="https://xxx";

$body = array(
    'auth_token' => 'xxxxxx',
    'list_id' => 'xxxxx,
    'name' => 'Office',
    'campaign_id' => 'xxxxx',
);

$response = wp_remote_post($url, array(
    'body'=>$body, 
    'sslverify' => false // this is needed if your server doesn't have the latest CA certificate lists
    ) );

if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
    // error handling goes here
}

$results = wp_remote_retrieve_body( $response );
// $results has the actual results in it

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *