CURL set CURLOPT_POST perminetelly - php

I'm getting a very strange behavior when I do multiple requests with curl. Here's the function I have:
function http_request($curl, $url, $post = null)
{
echo (isset($post) ? 'POST ' . $url : 'GET ' . $url) . PHP_EOL;
try
{
$cookie_path = tempnam(null, 'b');
curl_setopt_array($curl, array
(
CURLOPT_COOKIEFILE => $cookie_path,
CURLOPT_COOKIEJAR => $cookie_path,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => count($post),
CURLOPT_POSTFIELDS => $post,
CURLOPT_HEADER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
CURLOPT_HTTPHEADER => array
(
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
)
));
return curl_exec($curl);
}
catch(Exception $exception)
{
echo $exception;
}
}
Here's the code I use to call it:
$curl = curl_init();
http_request($curl, "http://host.com/url1");
http_request($curl, "http://host.com/url2", "postdata=123");
http_request($curl, "http://host.com/url3");
http_request($curl, "http://host.com/url4");
curl_close($curl);
This is the output I'm getting:
GET http://host.com/url1
POST http://host.com/url2
GET http://host.com/url3
GET http://host.com/url4
So far so good, but using packet analyzer (wireshark), the output looks like this:
POST http://host.com/url1
Content-Length: 0;
POST http://host.com/url2
Content-Length: 12;
POST http://host.com/url3
Content-Length: 0;
POST http://host.com/url4
Content-Length: 0;
Then I rewrote the code like this:
function http_request($curl, $url, $post = null)
{
echo (isset($post) ? 'POST ' . $url : 'GET ' . $url) . PHP_EOL;
try
{
$cookie_path = tempnam(null, 'b');
curl_setopt_array($curl, array
(
CURLOPT_COOKIEFILE => $cookie_path,
CURLOPT_COOKIEJAR => $cookie_path,
CURLOPT_COOKIESESSION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
CURLOPT_HTTPHEADER => array
(
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
)
));
if($post != null)
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
else
{
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
}
return curl_exec($curl);
}
catch(Exception $exception)
{
echo $exception;
}
}
but still the same thing happens, if I remove this code from the function:
if($post != null)
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
else
{
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
}
In packet analyzer I get:
GET http://host.com/url1
GET http://host.com/url2
GET http://host.com/url3
GET http://host.com/url4
It makes no sense how it gets set to POST on the first request, even my $post argument is not set. Thanks!

I vaguely remember I had the same problem some time ago. Try setting CURLOPT_HTTPGET explicitly as well:
if($post != null)
{
curl_setopt($curl, CURLOPT_HTTPGET, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
else
{
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
}

Related

laravel 7 keeps adding '/public/' to my api routes

I am trying to make a php curl request to my laravel api
Here's the curl function
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_HEADER, array('Accept:application/json', 'X-Requested-With:XMLHttpRequest' )
);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
print curl_error($curl);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
Here's where I call it :
require_once ADJEMINPAY_PLUGIN_DIR.'includes/class-adjeminpay-functions.php';
$url = "https://api.adjeminpay.net/v1/auth/getToken/";
$postData = array(
'firstName' => 'Lady',
'lastName' => 'Gaga',
'submit' => 'ok'
);
$result = CallAPI("POST", $url, $postData);
var_dump($result);
Here's the result :
string(454) "HTTP/2 301 date: Fri, 04 Dec 2020 12:12:03 GMT server: Apache location: https://api.adjeminpay.net/public/v1/auth/getToken content-length: 258 content-type: text/html; charset=iso-8859-1
Moved Permanently
The document has moved here.
"
Looks like laravel doesn't get that it's an api request and keeps changing the url to https://api.adjeminpay.net/ public/ v1/auth/getToken/ ; it also tries to redirect to https://api.adjeminpay.net/
Help kudasai TT
Sooo, turns out the syntax/version of curl I was using was defective/uncomplete.
Went over to PostMan and executed the same request then copipasted the php -curl code which looks like this :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'yourUrl',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"apikey": "qsmdlfjqs",
"application_id": "qsdlfqsmlj",
"grant-type": "qmdsfqsdf"
}',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
': ',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it works fine now.
Arigathanks \(^^)/
By the way How to generate PHP -curl code from POSTMAN.

I need to execute a curl request using php

Here is a query on the command line that works.
How can I get an answer to this request using PHP in the script?
curl -X GET https://megaservice.com/api/abonents -H 'X-AUTH-TOKEN: xxxxx-xxx-xxxxx' -H 'cache-control: no-cache'
This should work.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://megaservice.com/api/abonents",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"X-AUTH-TOKEN: xxxxx-xxx-xxxxx",
"cache-control: no-cache,no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
You can use cUrl:
$channel = curl_init('https://megaservice.com/api/abonents');
curl_setopt_array($channel, [
CURLOPT_RETURNTRANSFER => true, // Return curl-data to variable
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => [
'X-AUTH-TOKEN: xxxxx-xxx-xxxxx',
'cache-control: no-cache',
],
]);
$result = curl_exec($channel);
curl_close($channel); // Important thing - close channel for not load your server
var_dump($result); // Here's your result
You can post or get like this with curl,
function post($url, $data, $headerArray = ["Content-type:application/json;charset='utf-8'","Accept:application/json"])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ($headerArray){
curl_setopt($curl, CURLOPT_HTTPHEADER,$headerArray);
}
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function get($url,$userpwd = "",$headerArray = ["Content-type:application/json;charset='utf-8'","Accept:application/json"]){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($userpwd) {
curl_setopt($curl, CURLOPT_USERPWD, $userpwd);
}
if($headerArray){
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
}
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

Curl request works on console but not in PHP

This simple request works on my console
curl 'https://www.nike.com/en'
But in PHP (with or without options like headers, useragent ...) I get Access Denied 403
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.nike.com/en");
$result = curl_exec($ch);
Thanks
Well, this would work for you.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.nike.com/en");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 40000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
echo $result;
<?php
class Curl {
public static function makeRequest($url) {
$ch = curl_init();
$request_headers = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;',
'Accept-Encoding: gzip, deflate',
"Connection: keep-alive",
"Content-Type: text/html; charset=UTF-8",
];
$options = [
CURLOPT_URL => $url,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0",
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FILETIME => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HTTPHEADER => $request_headers,
CURLOPT_COOKIESESSION => true,
CURLOPT_ENCODING => "gzip, deflate, scdh",
];
curl_setopt_array($ch, $options);
$result['content'] = curl_exec($ch);
$result['header'] = curl_getinfo($ch);
$result['error'] = curl_error($ch);
return $result;
}
}
var_dump(Curl::makeRequest('https://www.nike.com/en')['header']);
Some website strictly check the request headers and return you encoded so .. I added request headers ad decoding method to decode the content

xml-rpc API request client

I have to get some account information from xml-rpc API at sippy softswitch.
http://support.sippysoft.com/support/solutions/articles/77553-understanding-authentication,
http://support.sippysoft.com/support/solutions/articles/107367-get-cdrs-of-an-account but I can't construct my code correctly.
The documentation is very limited and I can't understand a lot.
This is my current code:
$urlCdr = "https://portal.mcginc.com/xmlapi/xmlapi";
$post_data = array(
'username'=> $USER,
'password'=> $PASS,
);
$options = array(
CURLOPT_URL => $urlCdr,
CURLOPT_HEADER => true,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false, // for https
CURLOPT_USERPWD => $USER . ":" . $PASS,
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($post_data)
);
$ch = curl_init();
curl_setopt_array( $ch, $options );
try {
$raw_response = curl_exec( $ch );
} catch(Exception $ex) {
if ($ch != null) curl_close($ch);
throw new Exception($ex);
}
if ($ch != null) curl_close($ch);
$cdr = "raw response: " . $raw_response;`
It returns nonce realm qop and so on. What should I do after that. Sending this info again to the server?
Please try this:
function Callmethod(){
$url = "https://portal.mcginc.com/xmlapi/xmlapi";
$method = "getAccountCDRs";
$params = array('i_account'=>1);//1 is id account
$post = xmlrpc_encode_request($method, $params);
$ch= curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
curl_setopt($ch, CURLOPT_USERPWD, 'user' . ':' . 'password');
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post );
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error_no = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error_no !=0){
die("CURL Error {$curl_error_no} - {$curl_error}n");
}
if ($response_code != 200){
die("ERROR response code:{$response_code} - {$response}n");
}
return xmlrpc_decode($response);
}

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