This works from the command line how do I convert this to PHP I have tried anything but nothing seems to work.
curl -H "Authorization: Bearer APIKEY" https://www.gocanvas.com/apiv2/forms.xml -F 'username=XXXXXXX#XXXXXXXXXX.com'
Here is a function I have tried.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml?username=XXXXX#XXXXXXXXXX.com';
$ch = curl_init();
$jsonData = array(
'text' => ''
);
$jsonDataEncoded = json_encode($jsonData, true);
$header = array();
$header[] = "APIKEY";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
}
-F sends form fields in the POST data, not URL query parameters. So you need to put username=XXX into CURLOPT_POSTFIELDS, and it shouldn't be JSON.
You can give an associative array to CURLOPT_POSTFIELDS, and it will encode it automatically.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml';
$ch = curl_init();
$params = array(
'username' => 'username=XXXXX#XXXXXXXXXX.com'
);
$header = array(
'Authorization: Bearer APIKEY'
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($$result);
}
header('Content-type: text/html; charset=utf-8');
require 'phpQuery/phpQuery.php';
function debug($arr) {
echo '<pre>'. print_r($arr, true). '</pre>';
}
function getContent($url, $data = []) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookie.txt');
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
$urlAuth = 'https://auth.mail.ru/cgi-bin/auth';
$url = 'https://wf.mail.ru/officers';
$auth_data = [
'Page' => 'https://wf.mail.ru/',
'FakeAuthPage' => 'https://wf.mail.ru/auth',
'Login' => 'MyNail',
'Password' => 'MyPass',
'do_login' => ''
];
$data = getContent($urlAuth, $auth_data);
$data = getContent($url);
debug($data);
In the cookie, the data is written fine, but when I go to the page wf.mail.ru/officers content is not displayed.
Tell me what the error is. Thank.
Added error output, error: Empty reply from server. But why is the answer empty?
I added to getContent()
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
if (!empty($data))
curl_setopt($ch, CURLOPT_POST, true);
else
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
This is my function:
function postCurl($url, $jsql) {
$data = array('sql' => $jsql);//jsql is a json string
$headers = array('Content-Type: application/x-www-form-urlencoded');
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$output = curl_exec($ch);
return $output;
}
It always returns false. I've tried to make the same request with REST client and it works.
Check in the server whether curl is enabled or not!!
and use the below code
$data = array('name' => $_REQUEST['name']);
$ch = curl_init('www.example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
$xml_output = htmlentities($response);
I'm trying to make a script that log in into my Linkbucks account to get my current stats.
They provide and api, but only for creating links, what I need is you get the statistics.
Things I discovered:
First, you have to stay logged
To get the stats, the website makes a ajax call to: https://www.linkbucks.com/Profile.aspx?task=manageLinks&action=loadPublisherStats with a JSON post like this: {"month":"09/01/2015"} .
With this post its easy to get the information I need, the problem is that my script is not working.
I share the code with you, so please help me.
Any idea or solution, or whatever will be appreciated.
Here is my script:
<?php
$urlLogin = "https://www.linkbucks.com/Default.aspx";
$ch = getSource($urlLogin);
$fuente = curl_exec($ch);
$re = "/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $matches);
$re = "/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $validation);
$re = "/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $generator);
$post = array(
"ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Username" => "yourusername" ,
"ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Password" => "yourpassword" ,
"__VIEWSTATE" => $matches[1] ,
"__VIEWSTATEGENERATOR" => $generator[1] ,
"__EVENTVALIDATION" => $validation[1]
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$config['useragent'] = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return $ch;
}
function postData($url , $array) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$server_output = curl_exec($ch);
return ($server_output);
}
?>
This should work:
$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks";
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
$source = getSource($urlLogin, $useragent);
$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source);
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source);
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source);
$post = array(
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourUsername",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourPassword",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0,
"__VIEWSTATE" => $viewstate,
"__VIEWSTATEGENERATOR" => $generator,
"__EVENTVALIDATION" => $validation
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $useragent = null, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return curl_exec($ch);
}
function postData($url, $postArray) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
return $server_output;
}
function findStringByRegex($regex, $source) {
preg_match($regex, $source, $matches);
return $matches[1];
}
Ive cleaned up your code a bit, it was mostly good, you were just missing 2 post fields: ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x and ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y'
Finally, thanks to runz0rd I was able to make the code work!!
Thank you so much guys!!
Here is the final version of the code:
<?php
$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks";
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
$source = getSource($urlLogin, $useragent);
$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source);
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source);
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source);
$post = array(
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourusername",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourpassword",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0,
"__VIEWSTATE" => $viewstate,
"__VIEWSTATEGENERATOR" => $generator,
"__EVENTVALIDATION" => $validation
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $useragent = null, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return curl_exec($ch);
}
function postData($url, $postArray) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postArray));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://www.linkbucks.com/Profile.aspx?task=manageLinks&action=loadPublisherStats");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$fecha = json_encode( array( "month"=> "09/01/2015" ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fecha );
$server_output = curl_exec($ch);
return $server_output;
}
function findStringByRegex($regex, $source) {
preg_match($regex, $source, $matches);
return $matches[1];
}
?>
I want to post some data including a image.I am using curl in php for this.My code is below-
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'http://postacity.co.uk:8080/shine/pp/upload/');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'creatorid' => '119',
'userfile' => '#/temp/fgf.jpg',
'notice' => 'Image1',
'catid' => '1',
'title' => 'bookofzeus',
'boardid' => '332',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
else {
echo $response;
}
}
Every time i am getting the same error
Error: failed creating formpost data
Am i doing any mistake.I have searched for this problem but still no solution found.
The path to the file is the problem: '#/temp/fgf.jpg'
You should take a look at this bug gescription: Bug 50060 - failed creating formpost data if post array value starts with #
The solutions of this problem for example at php freaks.
And use absolute path to the files.
Refer this its working
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
"username"=>"foobar",
"password"=>"secret",
"submit"=>"submit"
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
Also refer this url's
http://joshhighland.com/blog/2010/11/27/using-curl-and-php-to-upload-files-through-a-form-post/
http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/