How to send POSTFIELDS to cURL correctly - php

I need to send POST data via cURL as shown in the picture.
image with POST data
i have this code
$data = [
'action' => 'order_cost',
'address' => 'http://91.211.117.3:720'
];
$query = http_build_query($data);
$url = "https://ap4.taxi/api/TaxiAPI.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvJFySHvqeKppEN9W',
)
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);
but I get an error
image with error
I have already tried many options. Postman sends POST normally and I receive the answer.
Please tell me I can not even imagine how this can be done.

As I see from your code you are sending just two fields by POST method (action and address)
Please show us a code of https://ap4.taxi/api/TaxiAPI.php where you process received data.

function execute_curl($url, $curlopt = array()){
$ch = curl_init();
$strCookie = session_name().'='.session_id().'; path=/';
session_write_close();
$default_curlopt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIE => $strCookie,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1"
);
$curlopt = $curlopt + $default_curlopt;
curl_setopt_array($ch, $curlopt);
$response = curl_exec($ch);
$errormsg = curl_error($ch);
$errorCode = curl_errno($ch);
$results = array();
if($errormsg)
{
$results['status'] = 'error';
$results['data'] = $errormsg;
$results['errorcodetxt'] = curl_error_codes($errorCode);
}
else
{
$results['status'] = 'success';
$results['data'] = $response;
}
curl_close($ch);
return $results;
}
$curlopt = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => 1);
$curlresponse = execute_curl($url, $curlopt);

Related

file_get_content don't work for special site

i want to get content of https://06fazmusic.com/ but file_get_content don't work.
$context = stream_context_create(array(
'http' => array(
'method' => "GET",
'follow_location' => false,
'header' => "Accept-Language: en-US,en;q=0.8rn" .
"Accept-Encoding: gzip,deflate,sdchrn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .
"Accept-Language:en-US,en;q=0.8" .
"Cache-Control:max-age=0" .
"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
)
));
$post_url='https://06fazmusic.com/omid-called-namaze-eshgh/';
$array = get_headers($post_url);
echo file_get_contents($post_url, false, $context);
I didn't use file_get_contents(). I used Curl type to get that page ex code is:
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo curl_get_contents($post_url);
it is working Output screen shot is :

POSTing "/orders" to the GDAX API fails silently

I am trying to POST "/orders" to GDAX using a simple PHP function. I am getting no response and no errors. Orders are not getting placed and there are no PHP errors/warnings.
Not sure where I am going wrong.
function gdaxPost ($path, $post_array){
$url = 'https://api-public.sandbox.gdax.com/'.$path;
// Sandbox API #1 - fake key
$key = "03cc35bd4fb48ardad8097e0a45f";
$secret = "ihGzWV+li8AweKcL+oMDUvBzlmq9fR7z6rKksg43VFcWA3zysg6TxM+gGhEn0wg==";
$passphrase = "jqer9jxgfa6qcl";
$time = time();
$data = $time."POST"."/orders";
$sign = base64_encode(hash_hmac("sha256", $data, base64_decode($secret), true));
$headers = array(
'CB-ACCESS-KEY: '.$key,
'CB-ACCESS-SIGN: '.$sign,
'CB-ACCESS-TIMESTAMP: '.$time,
'CB-ACCESS-PASSPHRASE: '.$passphrase,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_array));
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
return $res;
}
$path = 'orders';
$post_array = array(
"price" => "600",
"size" => "1.01",
"side" => "buy",
"type" => 'limit',
"time_in_force" => "GTC",
"product_id" => "BTC-USD"
);
print_r(json_decode(gdaxPost ($path, $post_array)),true);
EDIT: I fixed the code!
This is the updated working code:
function gdaxPost ($path, $post_array){
$url = 'https://api-public.sandbox.gdax.com/'.$path;
// Sandbox API #1 - fake key
$key = "03cc35bd4fb48ardad8097e0a45f";
$secret = "ihGzWV+li8AweKcL+oMDUvBzlmq9fR7z6rKksg43VFcWA3zysg6TxM+gGhEn0wg==";
$passphrase = "jqer9jxgfa6qcl";
$time = time();
$data = $time."POST"."/".$path.json_encode($post_array);
$sign = base64_encode(hash_hmac("sha256", $data, base64_decode($secret), true));
$headers = array(
'CB-ACCESS-KEY: '.$key,
'CB-ACCESS-SIGN: '.$sign,
'CB-ACCESS-TIMESTAMP: '.$time,
'CB-ACCESS-PASSPHRASE: '.$passphrase,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_array));
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
return $res;
}
$path = 'orders';
$post_array = array(
"price" => "600",
"size" => "1.01",
"side" => "buy",
"type" => 'limit',
"time_in_force" => "GTC",
"product_id" => "BTC-USD"
);
print_r(gdaxPost ($path, $post_array));

Unable to get feed content with CURL

I'm trying to get the content of this feed :
http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss
Here is my code :
$url = 'http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss';
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_MAXREDIRS => 10
);
$curl = curl_init($url);
curl_setopt_array( $curl, $options );
$content = curl_exec($curl);
curl_close($curl);
echo $content;
I tried many other CURL options but it doesn't work.
As the content is accessible through my browser, I suppose it can be done with PHP. But what is wrong with my code ? It seems like there is an exception with the server of this feed ?
Not sure, may be your breaking the cURL options and calling the URL.
Here a simple example, give it a try:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$content = get_data('http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss');
echo $content;

CURL is not providing any values

my below code of PHP is not working. I have created test user id & password which is shared in the code. The file is not having any value. But I see with this code it is edited every time when I execute this code.
<?php
$username = '8632465';
$password = 'basade41';
$loginUrl = 'https://cabinet.instaforex.com/partner/login';
//$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';
$config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
//$userAgent = $_SERVER['HTTP_USER_AGENT'];
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the
//protected content.
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'https://cabinet.instaforex.com/partner/aff_statistic');
//execute the request
$content = curl_exec($ch);
//save the data to disk
file_put_contents('c:/xampp/htdocs/upload/test.txt', $content);
?>
I think your code missing some settings. eg:CURLOPT_CAINFO...
This is my https code
<?php
//The CA certificate path
$caDir = '....../GeoTrustGlobalCA.crt';
//cookie file path
$cookieDir = '......';
//URL
$url = 'https://www.zhihu.com/......';
$defaultOpt = 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 => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36",
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
CURLOPT_SSL_VERIFYPEER => true, // enabled SSL Cert checks
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => $caDir,
CURLOPT_COOKIEFILE => $cookieDir,
CURLOPT_COOKIEJAR => $cookieDir,
CURLOPT_URL => $url,
CURLOPT_HTTPGET => true //method is GET
);
$handle = curl_init();
curl_setopt_array($handle,$arrOpt);
$content = curl_exec($handle);
$err = curl_errno($handle);
$errmsg = curl_error($handle);
$header = curl_getinfo($handle);
curl_close($handle);
printf("Content is %s", $content);
?>
you can refer to this article php-curl-https (in (^__^)) OR this using-curl-in-php-to-access-https-ssltls-protected-sites

php curl: I need a simple post request and retrival of page example

I would like to know how to send a post request in curl and get the response page.
What about something like this :
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://www.example.com/yourscript.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'field1' => 'some date',
'field2' => 'some other data',
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
// result sent by the remote server is in $result
For a list of options that can be used with curl, you can take a look at the page of curl_setopt.
Here, you'll have to use, at least :
CURLOPT_POST : as you want to send a POST request, and not a GET
CURLOPT_RETURNTRANSFER : depending on whether you want curl_exec to return the result of the request, or to just output it.
CURLOPT_POSTFIELDS : The data that will be posted -- can be written directly as a string, like a querystring, or using an array
And don't hesitate to read the curl section of the PHP manual ;-)
$url = "http://www.example.com/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'username' => 'foo',
'password' => 'bar'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$contents = curl_exec($ch);
curl_close($ch);
You need to set the request to post using CURLOPT_POST and if you want to pass data with it, use CURLOPT_POSTFIELDS:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$data = array(
'username' => 'foo',
'password' => 'bar'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$contents = curl_exec($ch);
curl_close($ch);
try the one in the comments: http://php.net/manual/en/curl.examples-basic.php
(but add curl_setopt($ch, CURLOPT_POST, 1) to make it a post instead of get)
or this example: http://php.dzone.com/news/execute-http-post-using-php-cu
<?php
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://example.com/student_list.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
?>
I think you need to add
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);
Using JSON DATA with CURL
client.php
<?php
$url="http://192.168.44.10/project/server/curl_server.php";
$data=['username'=>'abc','password'=>'123'];
$data = json_encode($data);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result; //here you get result like: username: abc and password: 123
?>
curl_server.php
<?php
$data = file_get_contents('php://input');
$Data= json_decode($data,true);
echo 'username: '.$Data['username']." and password: ".$Data['password'];
?>
Check out my code in this post
https://stackoverflow.com/a/56027033/6733212
<?php
if (!function_exists('curl_version')) {
exit("Enable cURL in PHP");
}
$url = "https://www.google.com/";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: " . url($url),
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
),
));
function url($url)
{
$result = parse_url($url);
return $result['host'];
}
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo "<textarea>" . $response . "</textarea>";
}
http://codepad.org/YE6fyzCA

Categories