• I’m creating a wordpress plugin and I’m having trouble getting a cURL call to function correctly.

    Lets say I have a page http://www.domain.com/wp-admin/admin.php?page=orders

    Within the orders page I have a function that looks to see if a button was clicked and if so it needs to do a cURL call to the same page (www.domain.com/wp-admin/admin.php?page=orders&dosomething=true) to kick off a different function. The reason I’m doing it this way is so I can have this cURL call be async.

    I’m not getting any errors, but I’m also not getting any response back. If I change my url to google.com or example.com I will get a response. Is there an authentication issue or something of that nature possibly?

    My code looks something like this.. I’m using gets, echos, and not doing async just for the ease of testing.

    Also, sorry for the bad question title, if you have a suggestion let me know so I can edit it.

    if(isset($_POST[‘somebutton’]))
    {
    curlRequest(“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);
    }

Viewing 1 replies (of 1 total)
  • Hi,

    You should add some authentication to your cURL options.
    Basically you need to use to 2 cURL calls, one to login to admin side of WordPress and another to call your admin page.

    $hCURL = @curl_init(); // create cURL handle ( )
    	if ( ! $hCURL ) {
    ?>
    <p>Couldn't initialize a cURL handle.</p>
    <?php
    	} else {
    		$bError = false;
    		$sCookieJar = '/tmp/cookies';
    		$sReferer = 'http://extranet.example.com.br/index.htm';
    		$sURL = 'http://extranet.example.com.br/cgi-bin/login.plx';
    		$sPost = 'login=' . urlencode( $_REQUEST['login'] ) . '&senha=' . urlencode( $_REQUEST['senha'] );
    		curl_setopt( $hCURL, CURLOPT_FAILONERROR, 1 );
    		curl_setopt( $hCURL, CURLOPT_HEADER, 0 );
    		curl_setopt( $hCURL, CURLOPT_RETURNTRANSFER, 0 );
    		curl_setopt( $hCURL, CURLOPT_TIMEOUT, 30 );
    		curl_setopt( $hCURL, CURLOPT_VERBOSE, 0 );
    		curl_setopt( $hCURL, CURLOPT_FOLLOWLOCATION, 0 );
    		curl_setopt( $hCURL, CURLOPT_COOKIEJAR,  $sCookieJar );
    		curl_setopt( $hCURL, CURLOPT_POST, 1 );
    		curl_setopt( $hCURL, CURLOPT_POSTFIELDS, $sPost );
    		curl_setopt( $hCURL, CURLOPT_URL, $sURL );
    		curl_setopt( $hCURL, CURLOPT_REFERER, $sReferer );
    		ob_start();
    		$sReturn = curl_exec( $hCURL );
    		ob_end_clean();
    		if ( curl_errno( $hCURL ) ) {
    			echo 'cURL error: <strong>' . curl_error( $hCURL ) . '</strong>';
    			$bError = true;
    		}
    		curl_close( $hCURL );
    		if ( ! $bError ) {
    			$hCURL = curl_init(); // create cURL handle ( )
    			if ( ! $hCURL ) {
    		?>
    <p>Couldn't initialize a second cURL handle.</p>
    <?php
    			} else {
    				$sReturn = '';
    				$sReferer = 'http://extranet.example.com.br/cgi-bin/login.plx';
    				$sURL = 'http://extranet.example.com.br/cgi-bin/monta_layout.plx?entrada=1';
    				curl_setopt( $hCURL, CURLOPT_RETURNTRANSFER, 1 );
    				curl_setopt( $hCURL, CURLOPT_COOKIEFILE, $sCookieJar );
    				curl_setopt( $hCURL, CURLOPT_URL, $sURL );
    				curl_setopt( $hCURL, CURLOPT_REFERER, $sReferer );
    				if ( false === ( $sReturn = curl_exec( $hCURL ) )  ) {
    					echo 'cURL error after login: <strong>' . curl_error( $hCURL ) . '</strong>';
    				}
    				curl_close( $hCURL );
    				if ( strstr( $sReturn, '<meta http-equiv="refresh" content="0;URL=/cgi-bin/relogin.plx?script=monta_layout.plx">' ) ) {
    					echo 'Login inválido';
    				} else {
    					// logged in, show content
    					echo ( $sReturn );
    				}
    			}
    		}
    	}
Viewing 1 replies (of 1 total)

The topic ‘How do you use curl within wordpress plugins?’ is closed to new replies.