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/...');
Related
I am trying to get info with soap web service but it is giving me an error Request format is invalid: multipart/form-data; boundary=------------------------b35e9c9f375db7a5.
$id = $_POST['id'];
$servicepassword = $_POST['servicePassword'];
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://webgov.com.tr/WebService/Kontrol.asmx/PasoSorguKontrol',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
'id' => $id,
'servicepassword' => $servicepassword
],
CURLOPT_FOLLOWLOCATION => 1,
));
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
this web service need two variables to work, student id and web service password. and giving xml output. i could not make the code working. i am a newbie btw.
The solution is using CURLOPT_HTTPHEADER. I even make a function with this codes and it solved my problem.
function httpPost($url,$params) {
$postData = '';
foreach($params as $k => $v) {
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
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);
}
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);
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);
?>
Please help me i am having some trouble.
What i want to do is integrate moneybooker in my website.
I studied the "Merchant Integration Manual version 6.17" in 2.3.2 Topic where i have to create and get a SESSION_ID form skrill server by sending payment parameters by post and get the SESSION_ID this session will hold my transaction information like amount and my account.
By using below code i can't retrieve the SESSION_ID from their server.
$url = "https:www.moneybookers.com/app/payment.pl";
$post_data = array (
"prepare_only" => 1,
"amount" => 10,
"currency" => 'USD',
"detail1_description" => "Description",
"detail1_text" => "Text",
"pay_to_email" => "****my**accoutn#yahoo.com"
);
$head = get_web_page($url, $post_data);
echo "<pre>";
print_r($head);
echo "</pre>";
function get_web_page( $url, $post_data )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//we are doing a POST request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post variables to the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
The above code works for other sites but not for moneybooker.
But when i submit simple html form, session id is created and shown but i am not being redirected to my site !!
Try adding cookies support
$fh = fopen("cookies.txt", "a+") or die("Can't open file!");
fclose($fh);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
When you set CURLOPT_POSTFIELDS with an array, the request will be sent as multipart/form-data, try changing that line to:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));