Php Curl Sequential function - php

Is this possible? We want to achieve this sequence of functions to integrate.I'm kinda new at php curl functions. The first line of code is what we want to achieve.
Here's our php:
<?php
paythrougPaypalSanbox() -> publishSite() -> redirectToCurrentSite()
header("Location:".$dashboard_link);
function paythrougPaypalSanbox() {
$data = $_GET['siteName'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
so on...
curl_close($ch);
}
function publishSite() {
$data = $_GET['siteName'];
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/publish/'.$siteName);
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
so on..
curl_close($ch);
}
function redirectToCurrentSite() {
$data = $_GET['siteName'];
curl_setopt($ch, CURLOPT_URL, 'http://trilogy.editor.multiscreensite.com/home/site/'.$siteName);
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
so on..
}
curl_close($ch);
return $dashboard_link;
}
?>

function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
return false;
}
curl_close($ch);
return $data;
}
if(curl($url)){
// second request
}

Related

CURLOPT_URL how to get two different links?

The below code works with API URL: https://api2.example.com/service/vps/list
but the API provider has changed, they use 2 URLs at the same time, for example: https://api2.example.com/service/vps/list and https://api2.example.com/service/dedicated/list
how to get vps and dedicated information from the above two links?
this is my code working with https://api2.example.com/service/vps/list only:
if (!defined("WHMCS"))
die("This file cannot be accessed directly");
use Illuminate\Database\Capsule\Manager as Capsule;
class FKL
{
public $apikey = '';
public $apiurl = 'https://api2.example.com/';
public function __construct($apikey = '')
{
$this->apikey = $apikey;
}
public function getList()
{
$sendparams = [];
$sendparams['APIKey'] = $this->apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/vps/list");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendparams, JSON_PRETTY_PRINT));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json')
);
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
$list = json_decode($result, true);
return $list['data'];
}
}
public function getOSList()
{
$sendparams = [];
$sendparams['APIKey'] = $this->apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/vps/os");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendparams, JSON_PRETTY_PRINT));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json')
);
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
$list = json_decode($result, true);
return $list['data'];
}
}
......
Thank you!
I have added code:
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/dedicated/list");
bellow:
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/vps/list");
and now are working!

PHP curl request return false

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);

PHP curl get multidimensional array

I'm trying to get a list of issues using gitlab API. The response is correct testing the URL, but the curl doesnot execute in my php code. I guess it`s because of the multidimensional array. but how do you solve this? Here is my function:
function getissues_list () {
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}
Thanks!
function getissues_list () {
global MAIN_URL; // Is this accessible in this function?
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
var_dump($response);// ADD THIS LINE TO DEBUG!!!!!!!!
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}

Send POST with cURL postfields to iframe

I am trying to send POST data using CURL to an url. This URL is called by an iframe, which is in my application.
<iframe frameborder="0" width="950" height="1000" src="/tools/costs"></iframe>
public function costs()
{
$url = "http://someurl.com/tools/costs";
if($_POST)
{
$fields_str = http_build_query($_POST);
$query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D',$fields_str);
$realstring = str_replace("%5D", "", str_replace("%5B", "", $query));
// $realstring echo:
var_carrier=3&var_carrier=5&var_carrier=6&var_service=Demurrage+%2F+Storage&var_service=Detention&var_pod=Ancona&var_pod=Antalya&var_pod=Antwerpen
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$realstring);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
else
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
$fields_str = "";
}
echo $response;
}
but the $realstring variable is empty when i send it by cURL POSTFIELDS
I have no idea wy!

PHP, CURL: function get the response code and return in variable

I have the following function:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: text/plain');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
I need to have some logic inside this function that if the response code of the URL is for example 404, then return null $data variable. I made an example of what I am looking for:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: text/plain');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
if (empty($data) or (HTTP response code is 404)) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch);
$data = null;
return $data;
} else {
// everything is ok
return $data;
}
}
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: text/plain');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
if (empty($data) OR (curl_getinfo($ch, CURLINFO_HTTP_CODE == 404))) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch);
$data = null;
return $data;
} else {
// everything is ok
return $data;
}
}
use curl_getinfo($ch) before curl_close() called

Categories