1

I am trying to interface with the Clickbank API for support ticket creation (https://api.clickbank.com/rest/1.2/tickets/).

I can't seem to get it to work with CURL. I can get the API working fine using GET (fetching order information), but not POST (required for changes to orders).

Here's my code, which returns an error 404 from Clickbank.

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.2/tickets/FNKBEP34?type=cncl&reason=ticket.type.cancel.7");
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: removed_dev_key:removed_api_key"));
    $result = curl_exec($ch);
    curl_close($ch);

    print $result;
?>

I've been able to POST successfully with RESTClient2.3. But I haven't been able to duplicate that with CURL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Woodhams
  • 11,977
  • 10
  • 50
  • 52
  • With HTTP POST the query string goes in the request body, not in the url. Take out the `?type=cncl&reason=ticket.type.cancel.7` from the url and instead put it into the POST body (without the `?` prefix). I can't recall off the top of my head the exact PHP curl syntax. – Asaph May 05 '11 at 01:00
  • should you be taking your query string params (type=cncl&reason=ticket.type.cancel.7) out and make them the request's post body? – Ben May 05 '11 at 01:00
  • @Ben: Yes. That's why his code is not working. – Asaph May 05 '11 at 01:02
  • Here is the error message I am getting on now that I added curl_setopt($ch, CURLOPT_POSTFIELDS, "type=cncl&reason=ticket.type.cancel.7"); "The clickbank api does not support parameters passed in within request body. Please use query parameters instead." – Nick Woodhams May 05 '11 at 01:07
  • Same error when I make a Data Array for POSTFIELDS... – Nick Woodhams May 05 '11 at 01:14
  • I would guess that it only supports get then... – Hailwood May 05 '11 at 01:15
  • No, it supports POST. But only with Query Parameters. I'm able to update the order successfully with RESTClient2.3. – Nick Woodhams May 05 '11 at 01:21
  • 1
    possible duplicate of [How to forward $\_POST with PHP and cURL?](http://stackoverflow.com/questions/2725488/how-to-forward-post-with-php-and-curl) –  Dec 18 '13 at 09:11

3 Answers3

1

My wild guess is that RestClient tries to be smart and actually transforms your get URL parameters to post variables on the fly (I could be wrong though).

Anyhow... POST, you're doing it wrong.

A curl POST request should be something like this:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.2/tickets/FNKBEP34");
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);

    //Take this out: curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "type=cncl&reason=ticket.type.cancel.7");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: removed_dev_key:removed_api_key"));
    $result = curl_exec($ch);
    curl_close($ch);

    print $result;
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben
  • 20,737
  • 12
  • 71
  • 115
  • I tried this example but getting below error: HTTP/1.1 400 Bad Request Date: Thu, 14 Aug 2014 07:13:52 GMT Server: Apache/2.2.27 (FreeBSD) mod_jk/1.2.40 mod_ssl/2.2.27 OpenSSL/0.9.8y Vary: Accept-Encoding Connection: close Transfer-Encoding: chunked Content-Type: text/plain A post method was executed with a request body. (Possibly post parameters). The clickbank api does not support parameters passed in within request body. Please use query parameters instead. Request Body: ''1
    I am tired of this almost spent a week on this but no luck please help me.
    – Krishna Aug 14 '14 at 07:14
  • @Krishna Post a separate Stack Overflow question – Ben Aug 14 '14 at 07:32
  • Hi Ben, i posted this as seperate question here http://stackoverflow.com/questions/25304556/404-not-found-error-while-calling-clickbank-webservices please help me to get out of it. Thanks. – Krishna Aug 14 '14 at 09:25
1

After about 10 and a half hours of trial and error... I figured it out.

The problem was two prong.

  1. I am running PHP 5.2.4 which has a bug accepting custom headers. I found this code, which worked perfectly.

     if(version_compare(PHP_VERSION, '5.3.0') == -1){
         ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $options['header']);
     }
    
  2. Curl would not work. Period. I tried everything. I really have no idea why.

Here is the final code I ended up with, which works perfectly.

This interfaces with the Clickbank API via POST. I hope this helps a lot of people.

<?
    $options = array(
      'http' => array(
        'method' => "POST",
        'header' =>
          "Accept: application/xml\r\n" .
          "Authorization: DEV-key-here:API-key-here\r\n"
        )
    ));

    if(version_compare(PHP_VERSION, '5.3.0') == -1){
        ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $options['header']);
    }

    $context = stream_context_create($options);

    $xml = file_get_contents('https://api.clickbank.com/rest/1.2/tickets/VZR9RYE3?reason=ticket.type.cancel.7&type=cncl', false, $context);
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Woodhams
  • 11,977
  • 10
  • 50
  • 52
  • Interesting. Thanks for sharing your feedback – Ben May 05 '11 at 09:00
  • I tried this but doesn't work it gives me just blank screen. I posted a seperate question here http://stackoverflow.com/questions/25304556/404-not-found-error-while-calling-clickbank-webservices for this, Please help me. – Krishna Aug 14 '14 at 09:28
0
<?php
    function executeCurl($arrOptions) {

        $mixCH = curl_init();

        foreach ($arrOptions as $strCurlOpt => $mixCurlOptValue) {
            curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);
        }

        $mixResponse = curl_exec($mixCH);

        curl_close($mixCH);

        return $mixResponse;
    }

    // If you need any HTTP authentication

    $username = 'http-auth-username';
    $password = 'http-auth-password';

    $requestType = 'POST'; // This can be PUT or POST

    // This can be $arrPostData = $_POST;
    $arrPostData = array(
        'key1' => 'value-1-for-k1y-1',
        'key2' => 'value-2-for-key-2',
        'key3' => array(
                      'key31'  => 'value-for-key-3-1',
                      'key32'  => array(
                          'key321' => 'value-for-key321'
                          )
        ),
        'key4' => array(
            'key' => 'value'
        )
    );

    // You can set your POST data
    $postData = http_build_query($arrPostData); // Raw PHP array

    $postData = json_encode($arrPostData); // Only USE this when requesting JSON data

    $arrResponse = executeCurl(array(
        CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPGET => true,
        CURLOPT_VERBOSE => true,
        CURLOPT_AUTOREFERER => true,
        CURLOPT_CUSTOMREQUEST => $requestType,
        CURLOPT_POSTFIELDS  => $postData,
        CURLOPT_HTTPHEADER  => array(
            "X-HTTP-Method-Override: " . $requestType,
            'Content-Type: application/json', // Only USE this when requesting JSON data
        ),
        // If required HTTP authentication, use the below lines
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
        CURLOPT_USERPWD  => $username . ':' . $password
    ));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    please do not copy paste the same answer over. Mark this question as a duplicate instead –  Dec 18 '13 at 09:12
  • user2140173's comment refers to [this answer](https://stackoverflow.com/questions/2725488/how-can-i-forward-post-with-php-and-curl/20652171#20652171). – Peter Mortensen Feb 10 '21 at 00:10