I'm trying now to execute PUT request using cURL PHP.
Unfortunately, I receive an error "HTTP Status 415 - Unsupported Media Type".
Here is my code :
$ch = curl_init();
$proxy = 'api.test.sandbox.mobile.de';
$proxy_port = "8080";
$loginpassw = 'XXX:YYY';
$url='https://services.mobile.de/seller-api/sellers/1086/ads/509939';
$headers = array();
$headers[] = "Content-type: application/json";
$data = array(
'mileage' => '10000',
);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo $data;
curl_close($ch);
What is wrong with py request ?
According to their API, you need to send data in either JSON or XML format and specify Content-Type header
https://services.mobile.de/docs/seller-api.html#_media_types
So set the header for writing:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/vnd.de.mobile.api+json');
And change POSTFIELDS to
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
My coding is this
$number1= $_POST{'username'};
set_time_limit(0);
error_reporting(0);
$post = array(
'phone_number'=>$number1);
$url="https://www.pokketdeal.com/lokiapp/otp";
$headers[]='Content-Type: application/json';
$headers[]='Content-Length: 31';
// $headers[]='X-Mclient: 3';
// $headers[]='x-wap-profile: http://www.1066.cn/uaprof/prof/Micromax/Micromax_A177.xml';
// $headers[]='X-Requested-With: com.pokkt.app.pocketmoney';
$headers[]='Accept-Encoding: gzip,deflate';
$headers[]='Accept-Language: en-US';
$headers[]='Accept-Charset: utf-8, utf-16, *;q=0.7';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_USERAGENT, "okhttp/2.3.0" );
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
echo $html. "<br>";
when submitting data i am getting server response as
HTTP/1.1 400 BAD REQUEST Server: nginx/1.4.6 (Ubuntu) Date: Thu, 10 Dec 2015 12:16:40 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Frame-Options: SAMEORIGIN
Bad Request (400)
also my content type is application/json but response is text/html.
I am trying to log in to imdb.com with curl and then submit a rating for a movie. I'm aware that this goes against their ToS but I'm not building an application or anything, just a small script for personal usage. I'm new to curl but I got the login part working by using information found on stackoverflow. After I login I set the curl URL to http://www.imdb.com/ratings/_ajax/title, because that is where a rating is submitted. However, when I execute the curl commands, no rating is submitted. Not sure how to troubleshoot this either, so hopefully somebody can point me in the right direction? Here's what I got so far:
// options
$username = 'username';
$password = 'password';
$url_login = "https://secure.imdb.com/register-imdb/login";
$url_rating = "http://www.imdb.com/ratings/_ajax/title";
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$cookie_file_path = dirname(__FILE__)."/cookies.txt";
$agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
// get login page
$ch = curl_init();
// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// log
$verbose = fopen("loginfetch.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set first URL
curl_setopt($ch, CURLOPT_URL, $url_login);
// execute session to get cookies and required form inputs
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
// grab the hidden inputs from the form required to login
$fields = getFormFields($return);
$fields['login'] = $username;
$fields['password'] = $password;
// set postfields using what we extracted from the form
$postfields = http_build_query($fields);
// post to login page
$ch = curl_init();
// set post options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_URL, $url_login);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// log
$verbose = fopen("loginpost.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// perform login
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
//submit rating
$data['tconst'] = 'tt1709143';
$data['rating'] = '5';
$data['tracking_tag'] = 'title-maindetails';
$post = http_build_query($data);
// post to submit page
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url_rating);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// log
$verbose = fopen("ratingsubmit.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
After enabling logging I get three logs. The third one logs the request to the IMDB rating page, which does not work as you can reed in the log below:
* About to connect() to www.imdb.com port 80 (#0)
* Trying 72.21.203.211... * connected
* Connected to www.imdb.com (72.21.203.211) port 80 (#0)
> POST /ratings/_ajax/title HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36
Host: www.imdb.com
Cookie: cs=ECxJtiNrhA/m+SuIKh15AweBbbqgkVqM8BkNuqOS/TKzsu7Z84JeKeCRXRoA0U26oKcq7CWRbbqj9TkMh9HN2eCRWyxAGW26oKdbraCRbbqgsW26oJFt+uDBHYqg==; cache=BCYqeti-w3RKC8bV21R-BwArPk4ILOkGu0T6E1oB5KGihmddDp_kluyca1x7QLflsfnEZ9smi6EZc2uHo7eY5FZeXfG4EQ97tKKFR8VhyAW4d4Q; id=BCYiSjnWuGQc8HDlo5OAY8cDzxQyS5nHJqLgwq_9yI08DAjTU5l0CeOXL8dUvE28QUv1MNlBQ0MD5jEzs8OuhUVQKukg_AtlD58ORFostzT-mCzLCuv8a_mOFztCRGX7V3rpONDCl_xyKHAEj2JLSnWHI8VbKrpes93j5xsgNtdgeU0oYH3s93XMeRVWOM06V1Lg; session-id-time=1551966508; session-id=357-4286508-9576651; uu=BCYvAfd_f2bQLnYdtpdRlYkDth4AKSl6zlKVXzyzSLlagoM-bH3kvZe3FLFOj_KmoWbEkh-dRXiPZZtStWC72Dbsd6jCQiNnXDAyxc-_vmzg5yiJLuwbKVF6nICv9xuwCV_Gn-_Ek8gqTujYDQPdgIWR2Y3aXArES1RzXoqX1pA9jkZ1EkWFkVKNaukvSqxPQRJhE50xfMNMwaUJLJ8SLA1WRsIVLqp873yNvZf7ecyLd4hgmC7AxdbfzPtDCdwgaelx
Accept: */*
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 56
< HTTP/1.1 400 Bad Request
< Date: Sat, 08 Mar 2014 13:48:30 GMT
< Server: Server
< X-Frame-Options: SAMEORIGIN
< Content-Type: text/html;charset=UTF-8
< Content-Language: en-US
< Vary: Accept-Encoding,User-Agent
* Replaced cookie cache="BCYs4XPUvL_p2AL_pctQP7qEdwB9nBAXcIkiNxRZlqHtp9VjHkCy-GzvEIqsHCBHjjuGdWIyzZb1%0D%0Aip5WAl_SmYCtFg%0D%0A" for domain imdb.com, path /, expire 3541770158
< Set-Cookie: cache=BCYs4XPUvL_p2AL_pctQP7qEdwB9nBAXcIkiNxRZlqHtp9VjHkCy-GzvEIqsHCBHjjuGdWIyzZb1%0D%0Aip5WAl_SmYCtFg%0D%0A; Domain=.imdb.com; Expires=Thu, 26-Mar-2082 17:02:38 GMT; Path=/
< P3P: policyref="http://i.imdb.com/images/p3p.xml",CP="CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC "
< Cneonction: close
< Transfer-Encoding: chunked
<
* Connection #0 to host www.imdb.com left intact
* Closing connection #0
I figured out what went wrong. There was a missing string that IMDb expects to be submitted along with the movie ID and rating. It's called "Auth" and it is a string that is present on the movie movie page. So I added a function that looks for the auth string and pass this when submitting a rating to the IMDb. No more error.
Here's the whole (working) thing in case anyone is interested:
// options
$username = 'username';
$password = 'password';
$url_login = "https://secure.imdb.com/register-imdb/login";
$url_rating = "http://www.imdb.com/ratings/_ajax/title";
$movie_id = "tt1800241";
$url_movie = "http://www.imdb.com/title/" . $movie_id;
$data['tconst'] = $movie_id;
$data['rating'] = '7';
$data['tracking_tag'] = 'title-maindetails';
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$cookie_file_path = dirname(__FILE__)."/cookies.txt";
$agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
/**
Step 1: get login page and cookies
**/
$ch = curl_init();
// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("loginfetch.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_login);
// execute session to get cookies and required form inputs
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
/**
Step 2: post login credentials
**/
// grab the hidden inputs from the form required to login
$fields = getFormFields($return);
$fields['login'] = $username;
$fields['password'] = $password;
// set postfields using what we extracted from the form
$postfields = http_build_query($fields);
// post to login page
$ch = curl_init();
// set post options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("loginpost.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_login);
// perform login
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
/**
Step 3: get Auth string from movie page
**/
$ch = curl_init();
// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("authfetch.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_movie);
// execute session
$return_auth = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return_auth;
/**
Step 4: submit rating
**/
$data['auth'] = getAuth($return_auth);
$post = http_build_query($data);
// post to submit page
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("ratingsubmit.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_rating);
// execute session
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
function getFormFields($data)
{
if (preg_match('/(<form method="post.*?<\/form>)/is', $data, $matches)) {
$inputs = getInputs($matches[1]);
return $inputs;
} else {
return('Login form not found.');
}
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
// when submitting a rating to IMDb you also need to send an 'auth' string which we grab from the rating-list div on the movie details page
function getAuth($data)
{
if (preg_match('/data-auth="(.*?)"/is', $data, $matches)) {
$auth = $matches[1];
return $auth;
} else {
return('Auth string not found.');
}
}
Your cookie is empty when you submit the request. You need to close the curl handle every time you do curl_exec:
curl_close($ch);
This will store the cookies into the file.
So for your code you'll need to close it three times. Make sure you are initializing the curl again after closing, and also make sure you are pointing the cookie file accordingly every time.
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).
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.