How to get the HTTP method(like GET,POST,DELETE) used in the curl_php connections? It is not avaiable in curl_getinfo.
The code for the connection is
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
);
$response = curl_init($url);
curl_setopt_array($response, $options);
$content = curl_exec($response);
$options_debug = curl_getinfo($response);
curl_close($response);
By default cURL methods are always GET unless you specify it differently. Also, if you invoke cURL with -d or --data the request will be POST.
POST request override default GET when using -d parameter.
More info here: cURL info
BR
Related
Have made and deployed an app on an AWS EC2 instance, and accessing the app via ip_address:port, where ip_address is the elastic ip associated with the instance.
I am able to make API calls to this app using postman, and via cmd.
However, when I try to make POST request using PHP Curl on Wordpress (hosted on Bluehost), I keep getting the
Failed to connect to ip_address port XXXX after 0 ms: Connection refused.
Any ideas of what I might be missing?
'''php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http:ip_address:8000/payments/accounts/v1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_TIMEOUT => 1000,
CURLOPT_CONNECTTIMEOUT => 1000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_CUSTOMREQUEST => 'POST',));
print_r(curl_error($curl));
curl_exec($curl);
curl_close($curl);
'''
Getting this response
'''code
Failed to connect to id_address port 8000 after 0 ms: Connection refused
'''
Using the redacted ip and a slightly modified version of your code as follows:
$url='http://X.XXX.XX.XX:8000/payments/accounts/v1';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 1000,
CURLOPT_CONNECTTIMEOUT => 1000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST=>true
)
);
$res=curl_exec( $curl );
curl_close( $curl );
printf('<pre>%s</pre>',print_r( $res, true ));
Yields:
{"detail":"Authentication credentials were not provided."}
Perhaps if you were to actually include and submit some post data in the CURLOPT_POSTFIELDS option you might find a different response.
I'm trying to build a Connector to Dynamics 365 Business Central but I'm having problems getting the data. Please help me figure out why when I send a GET request using cURL and PHP, it produces the following output:
�S[O�0�+(��&��qB��i�t�F/���=�ا��Ďb�}N�"&�M�����������8�0% �;
Here is my code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.businesscentral.dynamics.com/{tenantID}/customers/?$filter=displayName%20eq%20'Shawn%20Test'",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: {Auth Code}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
It seems like a character encoding issue, or maybe you are receiving binary data.
What is this endpoint supposed to return? Check the response headers, it might give you a clue.
This is an encoding problem, try to remove CURLOPT_ENCODING line from your request or try to choose different encoding from your browser
Here is my code:
// Build request URL
$url = 'https://connect.squareup.com/v2/locations/';
// Build and execute CURL request
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
)
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
var_dump($content);
Here is what I get back:
string(158) "{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"NOT_FOUND","detail":"API endpoint for URL path `/v2/locations/` and HTTP method `GET` is not found."}]}"
I am pounding my head on this one... I tried using the Square SDK but calling from it doesn't return a list of locations either.
I have created an application on the Square Developer Dashboard. $accessToken is set to the sandbox access token listed there.
You have added an extra / at the end of the url. You should instead use:
$url = 'https://connect.squareup.com/v2/locations';
Other than that your code works!
I need some help, here the code :
<?php
$curl_options = array (CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 60,
CURLOPT_MAXREDIRS => 5 );
$handle = curl_init("http://my.awesome.subdomain.com");
curl_setopt_array($handle, $curl_options);
$content = curl_exec($handle);
print_r(curl_error($handle));
print_r($content);
die();
my.awesome.subdomain.com is a vhost. On the same machine i have another.awesome.subdomain.com which is on the same machine.
Via SSH, if i do $> curl my.awesome.subdomain.com I get the answer i expect. But when i use this code, i get "Connection timed out after 30180 milliseconds"
Thanks for help and sorry for my writing errors.
Before someone says something or "jump," I would like to say that I have read:
Any idea on how to scrape pages which are behind __doPostBack('...');?
DotNetNuke, PHP, Simulating a remote postback using curl
Scraping HTML with JavaScript postbacks
cURL post data to asp.net page
http://techclimber.blogspot.com.es/2009/03/php-curl-and-aspnet.html
This is my function:
function UolgetHtmlfromAjaxCallback($a_Params,$url) {
$EVENTTARGET = $this->UolgetAtributoEventTarget($a_Params['s_EventTarget']);
$s_smMaster = 'ctl00$cphSite$upModelo|'.$entries['target'];
$VIEWSTATE = urlencode($a_Params['s_ViewState']);
$options = 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 => "spider", // who am i
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_POST => true,
CURLOPT_POSTFIELDS => 'ct100%24smMaster='.urldecode($s_smMaster).
'&__EVENTTARGET='.urlencode($EVENTTARGET['target']).
'&__EVENTARGUMENT='.urlencode('').
'&__EVENTVALIDATION='.urlencode($s_Eventvalidation).
'&__VIEWSTATE='.$VIEWSTATE.
'&ct100%24txtBuscaNome='.$a_Params['ctl00_txtBuscaNome'].
'&__ASYNCPOST=true'
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$result = curl_exec ($ch);
return $result;
}
The $VIEWSTATE is okay, and the $EVENTTARGET is an array with these values:
Array (
[id] => ctl00_cphSite_fichaTecnicaEditorial_rptCarrosFichaTecnica_ctl00_lbtnFichaTecnica
[target] => ctl00$cphSite$fichaTecnicaEditorial$rptCarrosFichaTecnica$ctl00$lbtnFichaTecnica )
I'm trying to use this code to scrape this website:
http://comparecar.uol.com.br/Modelo/Volvo-Xc60
And I get the website, but not the car's information.
I edit my own information.
I'm working with Tamper Data and I found that the post uses different parameters:
ctl00%24smMaster
__EVENTTARGET
__EVENTARGUMENT
__VIEWSTATE
__EVENTVALIDATION
ctl00%24txtBuscaNome
__ASYNCPOST
And closes with an "="