CURLOPT_POST or CURLOPT_POSTFIELDS to send data - php

Should I use CURLOPT_POST or CURLOPT_POSTFIELDS to send data to server?
$curl = curl_init();
//this?
$post_data = array();
$post_data[] = "name: $name";
$post_data[] = "token: $token";
curl_setopt($curl, CURLOPT_HTTPHEADER, $post_data);
//or this?
$post_data = array();
$post_data['name'] = $name;
$post_data['token'] = $token;
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, "...");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_URL, $url );
$response = curl_exec($curl);
if (curl_errno($curl)) {
return(curl_errno($curl));
} else{
return $response;
}
curl_close($curl);

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!

Problem with Bearer Authorization token in php curl

When I try to post data with bearer token on https://reqbin.com there is no error. But I copy the code:
<?php
$url = "https://api.real-debrid.com/rest/1.0/unrestrict/link";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers["Authorization"] = "Bearer ZWP53ALWWN7*******GZHE66Z6BL54FV4Z2DWNS22URJUX6Q";
$headers["Content-Type"] = "text/plain";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "link=https://rapidgator.net/file/6e3e93b5c15700c574b4313de8ad416f/";
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
and try to run on my servers I get this error.
string(43) "{ "error": "bad_token", "error_code": 8 }"

How do I submit Data as Form-Data with cUrl

I have to send Data to an Api via PHP cUrl and then save the Token I get as a response.
In postman in works when I send the data as Form-data, but not if I send it as raw body. I now try to send the data via cUrl but I get the same response I get when I use postman with raw-body. How do I post it as form data?
Thanks
I tried those:
try:
$body = array();
$body['userEmail'] = $email;
$body['userPassword'] = $password;
$body['clientId'] = 'Kursapp';
$data_string = json_encode($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=UTF-8',
'Content-Type: application/json;charset=UTF-8'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec($curl);
curl_close ($curl);
try:
$body = array();
$body['userEmail'] = $email;
$body['userPassword'] = $password;
$body['clientId'] = 'Kursapp';
$data_string = http_build_query($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=UTF-8',
'Content-Type: application/json;charset=UTF-8'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec($curl);
curl_close ($curl);
try
$body = array();
$body['userEmail'] = $email;
$body['userPassword'] = $password;
$body['clientId'] = 'Kursapp';
$post_str ='';
foreach ($body as $key=>$value){
$post_str .= $key.'='.urldecode($value).'&';
}
$post_str = substr($post_str, 0, -1);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=UTF-8',
'Content-Type: application/json;charset=UTF-8'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postr);
$response = curl_exec($curl);
curl_close ($curl);
try with guzzle
$client = new Client();
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json;charset=UTF-8',
'Content-Type' => 'application/json;charset=UTF-8'
],
'form_params' => $body
]);

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

Retrieve curl data with client headers/IP

$url_in = 'http://vk.com/video_ext.php?oid=3145131&id=159485516&hash=d821df23b7dc0b54&hd=1';
function curl($url, $cookie = false, $post = false, $header = false, $follow_location = false)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow_location);
if ($cookie) {
curl_setopt ($ch, CURLOPT_COOKIE, $cookie);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$response = curl_exec ($ch);
curl_close($ch);
return $response;
}
$vk_video = curl($url_in);
preg_match('|host=(.*)&|Uis', $vk_video, $link1);
preg_match('|uid=(.*)&|Uis', $vk_video, $link2);
preg_match('|vtag=(.*)&|Uis', $vk_video, $link3);
$link= $link1['1'].'u'.$link2['1'].'/video/'.$link3['1'].'.360.mp4';
echo $link;
the problem is that vtag= is different for every ip client so how can i retrieve the vtag with client ip?

Categories