Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created July 1, 2019 13:16
Show Gist options
  • Save tommcfarlin/8762acc585d6b4987b23a1304615bd4d to your computer and use it in GitHub Desktop.
Save tommcfarlin/8762acc585d6b4987b23a1304615bd4d to your computer and use it in GitHub Desktop.
[WordPress] The Difference in cURL and WordPress Requests
<?php
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($curl);
if (\is_object($response)) {
return false;
}
if (false === $response) {
throw new Exception(curl_error($curl), curl_errno($curl));
}
curl_close($curl);
} catch (Exception $e) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
<?php
$response = wp_safe_remote_get($url);
if (is_wp_error($response)) {
return '';
}
$response = $response['body'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment