I can't log on to a page using curl - php

I need to log on a page using curl to get a value from the page, but I don't get any result at all.
Have I missed something or is it maybe impossible to implement?
<?
$u = "******************";
$p = "******************";
$token = file_get_contents("https://secure.izettle.com/portal/login");
preg_match('/content=\"(.*?)\" name=\"csrf-token/', $token, $t);
$authenticity_token = $t[1];
echo $authenticity_token;
$fields = array("user[email_address]" => $u, "user[password]" => $p, "authenticity_token" => $authenticity_token );
$ch = curl_init();
//Set curl options
$options = array(
CURLOPT_URL => "https://secure.izettle.com/portal/login",
CURLOPT_COOKIEJAR => "cookie.txt",
CURLOPT_COOKIEFILE => "cookie.txt",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://secure.izettle.com/portal/reports?date=2014-05-21");
$page = curl_exec($ch);
if(!curl_exec($ch)){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo $page.'<br>';

Is it possible for split up the code and try. Not sure it will work. Assumes the second form method is "post"
<?php
$u = "******************";
$p = "******************";
$fields = array("user[email_address]" => $u, "user[password]" => $p);
$ch = curl_init();
//Set curl options
$options = array(
CURLOPT_URL => "https://secure.izettle.com/portal/login",
CURLOPT_COOKIEJAR => "cookie.txt",
CURLOPT_COOKIEFILE => "cookie.txt",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_FOLLOWLOCATION =>true
);
curl_setopt_array($ch, $options);
$res = curl_exec($ch);
unset($ch);
//*****************************************************************************
$qry_str = "?date=2014-05-21";
$ch = curl_init();
// Set query data here with the URL
curl_setopt($ch, CURLOPT_URL, 'secure.izettle.com/portal/…' . $qry_str);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
print_r($content);
?>

Related

Upload a Pdf file to another website using cURL

Specifically relating to this particular thread Upload a file to a website using php I want to implement this using cURL on PHP.
How do I set this up?
Below is what I have tried.
Note I just edited this by adding the codes I have tried.
<?php
if(isset($_POST["submit"])){
$errors= array();//
$name = $_FILES['fileToUpload']['name'];
$size = $_FILES['fileToUpload']['size'];
$type = $_FILES['fileToUpload']['type'];
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
$upload_ok =1;
$file_ext=strtolower(end(explode('.',$_FILES['fileToUpload']
['name'])));//
$extensions= array("pdf","jpeg","jpg","png");
$jj = file_get_contents($_FILES['fileToUpload']['tmp_name']);
$mj = base64_encode($jj);
if(in_array($file_ext,$extensions)=== false){
$errors[]="vvvvv.";
}
if($size >= xxx){
$errors[]='xxx;'
}
if(empty($errors)==true){
echo "Success </br>";
$request = "xxxxxxxxx";
$headers = array(
"Authorization: Bearer xxxxxx",
"Content-type => multipart/form-data",
"Accept:application/json",
);
$data = [
'voucher_data' => 'base64_encode($tmp_name)',
'callback_url' => 'xxxxxxxxx'
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "xxxxxxxxxx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($curl);
curl_close($curl);
var_dump($response);
}else{
print_r($errors);
}
?>
I tried using curl_file_create($mj) as suggested below but on var_dump...it says bool(false)
I see that you want to make a request that uploads a file to another server other than your own.
After PHP 5.5, there is a function named curl_file_create and as the name suggests, it's being used for uploading files.
$post = array (
'extra_info' => '123456', // If required.. like user_id for avatar
'file_contents' => curl_file_create ('path/to/file');
);
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
curl_close ($ch);
If you don't want to use that then there is a much simple way:
$args = [];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
$args['file'] = '#/path/to/file';
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_close ($ch);
Well after a lot of trying out different methods I passed this value to the post field directly without using it as array
CURLOPT_POSTFIELDS => “foo=$mj&callbackurl=url”,
and it worked. This was where I was stucked. I did it the other way and it didn't work. Each situation is unique. Someone could also need this.

PHP cURL body does not work with multiple arrays

I need to send a data via cURL in PHP.
There is my POST request and it works with this example (I can post 1 record).
$postData = array(
"username" => "email#domain.com",
"name" => "Name",
"surname" => "Surname",
'role' => 'user',
"disabled" => "false"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt_array($ch,
array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
// ,CURLOPT_POSTFIELDS => json_encode($postData)
));
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
return stripslashes($response);
curl_close($ch);
But I would like to add records from database into $postData array but then it's stops.
This is my database array example:
$postData = array();
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
array_push($postData, $row);
}
json array looks ok but I can't find an error. Or i can't post multiple data and i need to add everything in a loop?
I find a few flaws in the code. You said that you are posting the code but you have set
CURLOPT_POST => false
Also, you have repeated
CURLOPT_RETURNTRANSFER => TRUE and CURLOPT_RETURNTRANSFER => 1
Also, your API might be designed to accept one record but you are trying to send multiple records. If it is designed to accept 1 record at a time you should make the curl call a function and call it in place of array_push. eg:. curlfunction($row);
function curlfunction($postData){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST , 1);
curl_setopt_array($ch, CURLOPT_HTTPHEADER, array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
curl_close($ch);
return stripslashes($response);
}
and
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
curlfunction($row);
}

xml-rpc API request client

I have to get some account information from xml-rpc API at sippy softswitch.
http://support.sippysoft.com/support/solutions/articles/77553-understanding-authentication,
http://support.sippysoft.com/support/solutions/articles/107367-get-cdrs-of-an-account but I can't construct my code correctly.
The documentation is very limited and I can't understand a lot.
This is my current code:
$urlCdr = "https://portal.mcginc.com/xmlapi/xmlapi";
$post_data = array(
'username'=> $USER,
'password'=> $PASS,
);
$options = array(
CURLOPT_URL => $urlCdr,
CURLOPT_HEADER => true,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false, // for https
CURLOPT_USERPWD => $USER . ":" . $PASS,
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($post_data)
);
$ch = curl_init();
curl_setopt_array( $ch, $options );
try {
$raw_response = curl_exec( $ch );
} catch(Exception $ex) {
if ($ch != null) curl_close($ch);
throw new Exception($ex);
}
if ($ch != null) curl_close($ch);
$cdr = "raw response: " . $raw_response;`
It returns nonce realm qop and so on. What should I do after that. Sending this info again to the server?
Please try this:
function Callmethod(){
$url = "https://portal.mcginc.com/xmlapi/xmlapi";
$method = "getAccountCDRs";
$params = array('i_account'=>1);//1 is id account
$post = xmlrpc_encode_request($method, $params);
$ch= curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
curl_setopt($ch, CURLOPT_USERPWD, 'user' . ':' . 'password');
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post );
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error_no = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error_no !=0){
die("CURL Error {$curl_error_no} - {$curl_error}n");
}
if ($response_code != 200){
die("ERROR response code:{$response_code} - {$response}n");
}
return xmlrpc_decode($response);
}

I want to recive the error code. by the sms host server in php

i want to receive errors from the sms host server
curl_error curl_errno curl_strerror all these function returns error but not in the way i want , mysmshost say that you will receive code like, 202 for wrong mobile number, 301 for wrong authentication, i want to receive those error code please help.
this is my code.
//sending message
$authKey = "my_authentication_key";
$senderId = "sender";
$message = urlencode("Hello Its Working..");
$route = "1";
$campaign = 'Default';
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $number,
'message' => $message,
'sender' => $senderId,
'route' => $route,
'campaign' => $campaign
);
//API URL
$url="http://sms.mysmshost.com/sendhttp.php";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
if(curl_errno($ch))
{
echo curl_error($ch)."-curl_error<br/>";
$chcode =curl_errno($ch);
echo $chcode;
echo '<br/>error:' . curl_strerror($chcode)."<br/>";
}
curl_close($ch);
echo $output;
Curl_errorno() is not executing, even if i print them without if condition they give no result.
Try this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);

curl not redirecting as expected

Any idea why this function is not redirecting to paypal.com and instead I am staying on the same page? curl is activated on the server, safe mode is off and openbasedir is turned off too.
function curl_post($url, array $post = NULL, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => http_build_query($post)
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
curl_post("https://www.paypal.com/cgi-bin/webscr",$_POST);
Any idea what am I doing wrong?
After you make curl request:
foreach ($parameters as $key => $value) {
$requestParams[] = $key . '=' . $value;
}
$requestParams = implode ('&', $requestParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER);
curl_setopt( $ch, CURLOPT_AUTOREFERER, true);
$result = curl_exec($ch);
You should get exact url, to know where you should be redirected:
$location = curl_getinfo($ch)['redirect_url'];
And then redirect:
header('Location: '. $location);
cURL is just a library for initiating and firing HTTP and FTP requests.
Your code just sends a HTTP POST request to the PayPal server.
In order to redirect the user, send a Location header (before any output!):
header('Location: https://www.paypal.com/...');

Categories