Curl Redirect Issue - php

0I'm trying to redirect the user to a 'Thank You' page on success, I'm using a simple php redirect.
header('Location: http://client.com/thank-you-one.php');
and I've added it into the snippet below but the redirect still isn't working, if anyone know why or can see where I'm going wrong I'd love some feedback.
$username = '*****';
$password = '*****';
//POPULATE THESE (and other fields) WITH REQUEST DATA
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['emailaddress'];
$company_name = $_POST['companyname'];
$date_of_birth = (''.$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'].'');
$loyalty_number = $_POST['membernumber'];
$host = 'http://api.client.com/users';
$user_curl = curl_init($host);
curl_setopt($user_curl, CURLOPT_HTTPHEADER, array('Accept: application/vnd.aardvark.crm+json; version=1.3'));
curl_setopt($user_curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($user_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($user_curl, CURLOPT_POST, 1);
curl_setopt($user_curl, CURLOPT_POSTFIELDS, http_build_query(array('forename' = > $first_name, 'surname' = > $last_name, 'email' = > $email, 'company_name' = > $company_name, 'date_of_birth' = > $date_of_birth, 'loyalty_number' = > $loyalty_number)));
curl_setopt($user_curl, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($user_curl);
$info = curl_getinfo($user_curl);
if ($info['http_code'] == '200') {
$data = json_decode($return, true);
$tag_curl = curl_init($host.'/'.$data['id'].'/tags/'.$_POST['favorite'].'');
curl_setopt($tag_curl, CURLOPT_HTTPHEADER, array('Accept: application/vnd.aardvark.crm+json; version=1.3'));
curl_setopt($tag_curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($tag_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($tag_curl, CURLOPT_POST, 1);
curl_setopt($tag_curl, CURLOPT_RETURNTRANSFER, TRUE);
$tag_return = curl_exec($tag_curl);
$ml_curl = curl_init($host.'/'.$data['id'].'/mailinglists/3');
curl_setopt($ml_curl, CURLOPT_HTTPHEADER, array('Accept: application/vnd.aardvark.crm+json; version=1.3'));
curl_setopt($ml_curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ml_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($ml_curl, CURLOPT_PUT, 1);
curl_setopt($ml_curl, CURLOPT_RETURNTRANSFER, TRUE);
$tag_return = curl_exec($ml_curl);
header('Location: http://client.com/thank-you-one.php');
echo 'test1';
exit;
} else {
//analyse resonse with
$data = json_decode($return, true);
print_r($data);
echo 'test2';
}
curl_close($user_curl);
curl_close($tag_curl);
curl_close($ml_curl);

I believe your issue is your testing for http_code 201. change it to 200
if ($info['http_code'] == '200') {

Related

PHP API Paging loop

I have this code
I need a loop to be able to print all results which are 400-600 but I'm not able because I'm limited to only 100 shows per page and I need a loop/while to make the same request but changing the page and then print the result and I'm not able to make it, can someone explain me how to implement here a loop or to give me some examples?
<?php
include('config/config.php');
$ch = curl_init();
$username = '**************';
$password = ''**************';';
$hash = base64_encode($username . ':' . $password);
$headers = array(
'Authorization: Basic ' . $hash
);
$query = $connection->query("SELECT id_xyz_ro as prods FROM stock_prods where id_xyz_ro > 0");
$array = Array();
while($result = $query->fetch_assoc()){
$array[] = $result['prods'];
}
$connection->query('SET session group_concat_max_len=15000');
foreach($connection->query("SELECT GROUP_CONCAT(CONCAT('\'', id_xyz_ro, '\'')) as prods FROM stock_prods where id_xyz_ro > 0") as $row) {
}
$datas = array(
'id' => $array,
'itemsPerPage' => '100', //Only 100 because API it's limiting me at 100 shows per page
'currentPage' => '1' //Here I need to loop
);
$data = array(
'data' => $datas
);
set_time_limit(0);
$url = "www.xyz.net/api-3/product_offer/read";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result); //After loop I need to print results from all loops
?>
Thanks in advance

VESTA, API, PHP. No user is added

I'm writing a script to add a new user to the hosting control panel "VESTA". I write according to the manual for api: http://vestacp.com/docs/api/ Errors when executing the code does not produce, but the user is not added to the VESTA. My code:
$vst_hostname = 'dfsdfl.csfdsdsgds.com';
$postvars = array(
$vst_username = 'zdeslogin',
$vst_password = 'zdesparol',
$vst_returncode = 'yes',
$vst_command = 'v-add-user',
$username = 'demo',
$password = 'password',
$email = 'demo#gmail.com',
$package = 'default',
$fist_name = 'Rust',
$last_name = 'Cohle'
);
$postdata = http_build_query($postvars);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
// Check result
if($answer == 0) {
echo "User account has been successfuly created\n";
} else {
echo "Query returned error code: " .$answer. "\n";
}
After running the code, the following line is returned: User account has been successfuly created.
The Russian community helped. Here is the code:
<?php
// Server credentials
$vst_hostname = 'sadsad.sadsad.com';
$vst_username = 'zdeslogin';
$vst_password = 'zdesparol';
$vst_returncode = 'no';
$vst_command = 'v-add-user';
$username = 'demo3';
$password = 'd3m0p4ssw0rd';
$email = 'demo#gmail.com';
$package = 'default';
$fist_name = 'Rust';
$last_name = 'Cohle';
$postvars = array(
'user' => $vst_username,
'password' => $vst_password,
'returncode' => $vst_returncode,
'cmd' => $vst_command,
'arg1' => $username,
'arg2' => $password,
'arg3' => $email,
'arg4' => $package,
'arg5' => $fist_name,
'arg6' => $last_name
);
$postdata = http_build_query($postvars);
// Send POST query via cURL
$postdata = http_build_query($postvars);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
// Check result
if($answer == 0) {
echo "User account has been successfuly created\n";
} else {
echo "Query returned error code: " .$answer. "\n";
}
?>

PHP cURL POST Request won't send?

I'm trying to update a mySQL database with cURL and PHP, but the values that are supposed to be sent by the post aren't inserted into the database. I don't get any errors and cURL is enabled according to phpinfo(). Here is my script:
<?php
$data = [
'email' => 'jsnow#got.com',
'token' => '58938539'
];
$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
And here's the receiver script:
<?php
require 'includes/db.php'; // Database connection. Connects with no errors raised
if(isset($_POST['email']) AND isset($_POST['token'])) {
$email = $_POST['email'];
$token = $_POST['token'];
$q = "INSERT INTO reset_tokens(email, token)";
$q .= " VALUES(?, ?)";
$stmt = $pdo->prepare($q);
$test = $stmt->execute([$email, $token]);
}
?>
Looks like you are missing HTTP Auth for http://www.econcentre.com/receiver.php.
You should include the HTTP Auth in the cURL request:
<?php
$data = [
'email' => 'jsnow#got.com',
'token' => '58938539'
];
$username = "auth-user";
$password = "auth-pwd";
$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);
?>

Return value from function where called

In the following custom function...
function get_txtlocal_balance() {
$username = variable_get('sms_txtlocal_email');
$hash = variable_get('sms_txtlocal_password');
$data = array('username' => $username, 'hash' => $hash);
$ch = curl_init('http://api.txtlocal.com/balance/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$balance = json_decode($response, true);
echo $balance['balance']['sms'];
}
I return a balance from a text local account. Now i try and echo this balance into place...
$balance = get_txtlocal_balance();
$title = 'SMS Integration - Current Balance: ' . $balance;
<span><?php echo $title;?></span>
But the returned value doesnt appear in the right place, it always returns are the top of the page, can anyone spot what im doing wrong?
Replace Below Code :
function get_txtlocal_balance() {
$username = variable_get('sms_txtlocal_email');
$hash = variable_get('sms_txtlocal_password');
$data = array('username' => $username, 'hash' => $hash);
$ch = curl_init('http://api.txtlocal.com/balance/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$balance = json_decode($response, true);
return $balance['balance']['sms'];
}

how can i use a foreach on a foreach php

hi guys i have this script but obviously im not using foreach right i was wondering is there anyway i could combine these two requests to works as one
$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000";
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$array = json_decode($rawdata,true);
foreach($array as $obj) {
$country = $obj['country'];
$countrycode = $obj['countryCode'];
}
foreach($countrycode as $did) {
$wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did";
$wgch = curl_init();
$wgtimeout = 0;
curl_setopt($wgch, CURLOPT_URL, $wgurl);
curl_setopt($wgch, CURLOPT_HEADER, 0);
curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout);
$wgrawdata = curl_exec($wgch);
curl_close($wgch);
$wgarray = json_decode($wgrawdata,true);
}
foreach($wgarray as $wgobj) {
$city = $wgobj['city'];
$citycode = $wgobj['cityCode'];
if($city){
$sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')";
database_queryModify($sql,$result);
}else{
echo "dint work";
}
}
there must be an easy way to do this im guesing creating another array from the data but i cant quite get it right i keep getting errors ive tried this and a few other things my problem is i need to cycle trough county codes and make requests from the code there a 150 requests i need to cycle trough to get all the city information and for each request i need to decode the json thats coming back and insert it into my city table
just put foreach inside each other like this
$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000";
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$array = json_decode($rawdata,true);
foreach($array as $obj) {
$country = $obj['country'];
$countrycode = $obj['countryCode'];
foreach($countrycode as $did) {
$wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did";
$wgch = curl_init();
$wgtimeout = 0;
curl_setopt($wgch, CURLOPT_URL, $wgurl);
curl_setopt($wgch, CURLOPT_HEADER, 0);
curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout);
$wgrawdata = curl_exec($wgch);
curl_close($wgch);
$wgarray = json_decode($wgrawdata,true);
foreach($wgarray as $wgobj) {
$city = $wgobj['city'];
$citycode = $wgobj['cityCode'];
if($city){
$sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')";
database_queryModify($sql,$result);
} else {
echo "dint work";
}
}
}
}

Categories