PHP CURL response includes HTTP Code and TIME etc - php

I am making a request with CURL using PHP
but I am receiving invalid response format in $result variable.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$param=json_encode($params);
$post_string = '{"method": "'.$method.'", "params":'.$param.' , "id": "0"}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result='';
$result = curl_exec($ch);
echo $result;
and Result is as following
HTTP/1.1 200 OK Content-Type: application/json Date: Fri, 21 Feb 2020 07:11:50 GMT Content-Length: 36 {"result":[],"error":null,"id":"0"}
but expected is
{"result":[],"error":null,"id":"0"}
I have tried to fetch it with json_decode but it is not working.
Any Suggestion>
Thanks in advance.

You explicitly asked for the headers to be included there.
https://www.php.net/manual/en/function.curl-setopt.php:
CURLOPT_HEADER - TRUE to include the header in the output.

Related

404 Not found error while calling clickbank webservices

error_reporting(E_ALL);
ini_set("display_errors", 1);
$ch = curl_init();
//$qry_str='?&type=RFND&comment="API refund check"&reason=ticket.type.cancel.7&refundType=FULL';
// curl_setopt($ch, CURLOPT_URL, 'https://api.clickbank.com/rest/1.3/tickets/N5GNE72J/'.$qry_str);
//https://api.clickbank.com/rest/1.3/tickets/RRBKQV4E/?type=rfnd&comment=&reason=ticket.type.refund.7&refundType=FULL
//curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.3/tickets/RRBKQV4E");
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.3/tickets/RRBKQV4E/?type=RFND&comment=&reason=ticket.type.cancel.7&refundType=FULL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '180');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml","Authorization:DEV-B4VTK7HDKAPDR9842SODK8GI49KAHTHL:API-59RD4F7BORDB7SELOII28DV0EMLIB3IT"));
$result = curl_exec($ch);
$errorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlInfo = curl_getinfo($ch);
$curlError = curl_error($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
When above code is run I am getting below error :
HTTP/1.1 404 Not Found
Date: Thu, 14 Aug 2014 08:14:19 GMT
Server: Apache/2.2.27 (FreeBSD) mod_jk/1.2.40 mod_ssl/2.2.27 OpenSSL/0.9.8y
Content-Length: 363
Vary: Accept-Encoding
Content-Type: text/html
Please someone help, I am struggling with this for last 1 week but no success.
After a long struggle found the solution. Use below code it worked for me.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://api.clickbank.com/rest/1.3/tickets/627JE7CZ/?type=rfnd&comment=&reason=ticket.type.refund.7&refundType=FULL");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
/**
* ClickBank doesn't allow POST parameters to be sent in; however, for the CURL POST to work correctly, the
* CURL_POSTFIELDS option must be set, so we'll just set it to empty and put all our request parameters on the URL
*/
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/xml",
"Authorization:DEV-enter your dev key here:API-enter your clerk key here"
));
$result = curl_exec($ch);
curl_close($ch);
?>

Clickbank API in php?

i need to get digital products from click bank, so am using their API to get products, unfortunately given example code given by them are not working, am using CURL to do this ,
code is below :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.clickbank.com/rest/1.3/products/list");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization:DEV-KEY:API-KEY"));
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>
but i got the below error
HTTP/1.1 400 Bad Request Date: Thu, 21 Feb 2013 05:20:47 GMT Server: Apache/2.2.23 (FreeBSD) mod_jk/1.2.37 mod_ssl/2.2.23 OpenSSL/0.9.8x Vary: Accept-Encoding Connection: close Transfer-Encoding: chunked Content-Type: text/plain The API call (/api/rest/1.3/products/list) requires parameters which are missing : [site]1
have any one got this error before ?
It's expecting a parameter called site. Please see the docs here:
https://api.clickbank.com/rest/1.3/products
Try This:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.clickbank.com/rest/1.3/products/list?site=<your unique affiliate id>");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization:DEV-KEY:API-KEY"));
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>

How to list out product details from click bank API and XML?

This is my sample code for click bank API unfortunately this script do not work
Here is the sample code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.3/products/list");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: DEV:CLERK"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
If run this code following error has occur
HTTP/1.1 400 Bad Request Date: Sat, 23 Feb 2013 05:24:10 GMT Server: Apache/2.2.23 (FreeBSD) mod_jk/1.2.37 mod_ssl/2.2.23 OpenSSL/0.9.8x Vary: Accept-Encoding Connection: close Transfer-Encoding: chunked Content-Type: text/plain The API call (/api/rest/1.3/products/list) requires parameters which are missing : [site]1
https://api.clickbank.com/rest/1.3/products
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.2/debug");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: DEV:CLERK"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
The required "site" parameter should be the Clickbank ID (also known as vendor).

Rest-ClickBank sandbox post api not working

I am trying to use Sandbox Api of clickBank which accepts a post request.
But somehow it doesnot work.
I am calling clickBank's Prepare Api (https://sandbox.clickbank.com/rest/1.2/sandbox/prepare) using a POST method.
But it giving me this error
HTTP/1.1 405 Method Not Allowed Date: Wed, 07 Nov 2012 12:08:32 GMT Server: Apache/2.2.22 (FreeBSD) mod_jk/1.2.32 mod_ssl/2.2.22 OpenSSL/0.9.8q Allow: POST,OPTIONS Content-Length: 1034 Content-Type: text/html;charset=utf-8 1
Here is my code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://sandbox.clickbank.com/rest/1.2/sandbox/prepare");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
//curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization:". $dev_key .":" .$api_key ));
$result = curl_exec($ch);
curl_close($ch);
print $result;
I tried everything but it doesnot seem to work.
Any help would be highly appreciated.
Thanks in Advance.
Try this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://sandbox.clickbank.com/rest/1.3/sandbox/prepare");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept:application/json", "Authorization: >>>Your Clickbank Developer API Key from ClickBan->Settings->My Account<<<"));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$return = curl_exec($ch);
curl_close($ch);
Try adding this before your curl_exec():
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
It worked for me, I hope it will work for you too.

How to Get Body content of HTTPS using CURL

The following code will retrieve the body content of a url retrieved using CURL in php but not https. Can anyone tell me how I edit the code as I need to get the data returned not just the header.
From the test I did here is the result. You can see it has a content-length, I just don't know how to access it.
Thanks
Stephen
Errors: 0
string(1457) "HTTP/1.1 200 OK Date: Sat, 01 Aug 2009 06:32:11 GMT Server: Apache/1.3.41 (Darwin) PHP/5.2.4 mod_ssl/2.8.31 OpenSSL/0.9.7l Cache-Control: max-age=60 Expires: Sat, 01 Aug 2009 06:33:11 GMT Last-Modified: Thu, 23 Nov 2006 17:44:53 GMT ETag: "97d620-44b-4565de15" Accept-Ranges: bytes Content-Length: 1099 Connection: close Content-Type: text/html "
<?php
$curl_handle=curl_init();
$username = "";
$password = "";
$fullurl = "http://www.queensberry.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, $fullurl);
$returned = curl_exec($ch);
curl_close ($ch);
var_dump($returned);
?>
Here is the solution:
Try this, just keep rest of the coding same as above...
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, $fullurl);
$returned = curl_exec($ch);
curl_close ($ch);
var_dump($returned);
Changing CURLOPT_HEADER to 0 makes it so that only the page content is returned.
I look for the length of the header using the curl's getinfo. Then substring the response:
$info = curl_getinfo($ch);
$start = $info['header_size'];
$body = substr($result, $start, strlen($result) - $start);
Shouldn't $fullurl be "https://www.queensberry.com" ?
When I changed $fullurl as stated and ran the code, var_dump displayed the "under construction" page.
if you still need the header, which means setting CURLOPT_HEADER to 0 is not an option, you can find the start of the body by looking for an empty line (two CRLF). See the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html
so this should do the job:
$data = curl_exec($ch);
$start = strpos($data, "\r\n\r\n") + 4;
$body = substr($data, $start, strlen($data) - $start);

Categories