php - How do you use curl within wordpress plugins? -


i'm creating wordpress plugin , i'm having trouble getting curl call function correctly.

lets have page www.domain.com/wp-admin/admin.php?page=orders

within orders page have function looks see if button clicked , if needs curl call same page (www.domain.com/wp-admin/admin.php?page=orders&dosomething=true) kick off different function. reason i'm doing way can have curl call async.

i'm not getting errors, i'm not getting response back. if change url google.com or example.com response. there authentication issue or of nature possibly?

my code looks this.. i'm using gets, echos, , not doing async ease of testing.

if(isset($_post['somebutton'])) {     curlrequest("http://www.domain.com/wp-admin/admin.php?page=orders&dosomething=true"); }  if($_get['dosomething'] == "true") {      echo("do something");      exit; }  function curlrequest($url) {     $ch=curl_init();     curl_setopt($ch, curlopt_url, $url);     curl_setopt($ch, curlopt_returntransfer, 1);     curl_setopt($ch, curlopt_connecttimeout, 5);     curl_setopt($ch, curlopt_timeout, 15);     curl_setopt($ch, curlopt_ssl_verifypeer, false);     $response = curl_exec($ch);     return($response);   }  

you're not supposed use curl in wordpress plugins.

instead use wp_ function issuing http requests, e.g.

function wp_plugin_event_handler () {     $url = 'http://your-end-point';       $foo = 'bar';     $post_data = array(          'email' => urlencode($foo));      $result = wp_remote_post( $url, array( 'body' => $post_data ) ); }  add_action("wp_plugin_event", "wp_plugin_event_handler"); 

in past i've run issues wordpress plugins event handlers hang curl. using wp_ functions instead worked expected.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -