This question already has answers here:
What is cURL in PHP?
(10 answers)
Closed 5 years ago.
In PHP, I see the word cURL in many PHP projects. What is it? How does it work?
Link http://php.net/curl
Curl is a command line tool for doing all sorts of URL manipulations and transfers, but this particular document will focus on how to use it when doing HTTP requests for fun and profit. I'll assume that you know how to invoke 'curl --help' or 'curl --manual' to get basic information about it.
Curl is not written to do everything for you. It makes the requests, it gets the data, it sends data and it retrieves the information. You probably need to glue everything together using some kind of script language or repeated manual invokes.
Taken from: https://curl.haxx.se/docs/httpscripting.html
we usually use it get the Return value of a Interface like this:
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
Related
This question already has answers here:
How do I send a POST request with PHP?
(18 answers)
Closed 2 months ago.
I am dealing with an affiliate marketing company and after I gather the info from a sale that was referred to my site they want me to "fire a postback" to their server in teh following format:
https://track.my affilatecompany.com/da.ashx?advertiserid=12345&clickid=&orderamount=&ordernumber="
I tried assembling the loaded URL in PHP and echoing it to the browser window with JavaScript but the affiliate company doesn't accept that. They have literally no information in their help docs, just says "fire a postback server-to-server". Any assistance would be most appreciated. Thank you.
You can use PHP Curl to make a POST request
This example will make the POST request to the affiliate.
You can use the $response variable to determine if the request was successful.
<?php
$url = "https://track.my affilatecompany.com/da.ashx?advertiserid=12345&clickid=&orderamount=&ordernumber=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
curl_close($ch);
?>
This question already has answers here:
How to get info on sent PHP curl request
(4 answers)
PHP - Debugging Curl
(8 answers)
Closed 1 year ago.
For example, I have a PHP handle that has been initialized somewhere.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
And at one point I want to know the executed raw curl request
curl_exec($ch);
// ???
some_method_to_get_raw_curl_request($ch);
Are there any built in methods that can be used for this? I tried looking in the PHP Curl Docs but can't seem to find any. If there is none, are there any other way to do this?
Ok I will try and keep this short. I'm making a request to a simple PHP API which can be done by AJAX through Javascript or JQuery, however I want to make the request using PHP. What is the best way to do this? Using the file_get_contents() function or CURL? If so how do I do it through CURL as the API requires I use GET not POST. Also the response I know is in XML, how do I then process the response once it comes back?
This question may have been asked many times, however when reviewing a lot of the questions and answers they are not specific to my needs on this one so please no answers with "Please see this link" as I can guarantee it won't answer the question in full as a lot of them are making requests from either Facebook API or another API that does not do what the API I am using does.
function getXML()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/index.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$res = curl_exec($ch);
return $res;
}
$xml_data = getXML();
$doc = new DOMDocument();
$doc->loadXML($xml_data);
$wms = $doc->getElementsByTagName('WowzaMediaServer');
$wmstotalactive = $wms->item(0)->getElementsByTagName("ConnectionsCurrent")->item(0)->nodeValue;
$wmstotaloutbytes = $wms->item(0)->getElementsByTagName("MessagesOutBytesRate")->item(0)->nodeValue;
so you extracted from xml the value from ConnectionsCurrent key and MessagesOutBytesRate
.
If your link does not need to auth remove :
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
I am trying to make a POST request to a URL using Curl but getting this error?
I'm trying to make a POST request to a server using curl with some XML included, but I'm getting the error "Request Entity Too Long" Could someone point out to me what I'm doing wrong? Thanks! :)
$url = 'http://web1.trueship.com/readyreturns/company/api.php/orders?key=xxxxxxxxxxxxxxxxxx&overwrite=true';
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("&xml=".$encodedXml));
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, TRUE);
$result = curl_exec ($curl);
curl_close ($curl);
print $result;
It seems to me this message means that the request your trying to process is larger than the server is willing or able to process. You will probably need to configure your php. ini differently
The setup value you will look for is "post_max_size"
if you want to see this value but don't have access to your php set up, write a simple php page with a <? phpinfo() ?>
tag in it, it will display the php.ini values of your current configuration. you can then contact your administrator to have it modified
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to read a web page in PHP
I'm sure there is some simple way to do this. I need to pass get variables through to my cart software to record a conversion, but not redirect the user, I just want the server to send GET variables to a URL. I'd rather not turn on allow_url_fopen in php.ini.
Anyone know the best way to do this? Thanks in advance.
Server side, your best option is probably to use cURL - see the documentation for details, it's not too difficult to use.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/script.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
if ($response === false) {
// Failed to connect or some error occurred
} else {
// Everything was fine, check the response here if you need to
}