When I use cURL to get a website and do strlen() on the result I get 72354. But when I pass the result through htmlspecialchars() and then do strlen() I get 0. Any ideas? Here's my code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/search?q=test');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
echo strlen($result);
echo '<br>';
echo strlen(htmlspecialchars($result));
try
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/search?q=test');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
echo strlen($result);
echo '<br>';
echo strlen(htmlspecialchars($result, ENT_QUOTES));
echo '<br>';
echo htmlspecialchars($result, ENT_COMPAT,'ISO-8859-1', true);
?>
Related
How to call more than one url in curl using php and must be executed sequentially one after the another..
Below is my php code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://192.168.1.220/cgi-bin/handle_login.tcl");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"user=admin&pw=admin&submit=Login");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo "$response";
curl_close ($ch);
$ch1= curl_init();
curl_setopt($ch1, CURLOPT_URL,"http://192.168.1.220/cgi-bin/controller.tcl?sid=$response&type=inverter&inverter=318002N463");
curl_setopt($ch1, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$response1= curl_exec($ch1);
curl_close ($ch1);
$ch2= curl_init();
curl_setopt($ch2, CURLOPT_URL,"http://192.168.1.220/cgi-bin/overview.tcl?sid=$response&menuParentId=3");
curl_setopt($ch2, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$response2= curl_exec($ch2);
curl_close($ch2);
Only my first curl command is executed remaining is not being executed
For sequential curl request you can define function and use it like this. If you are not concerned about sequential request you can use multi curl for that.
<?php
ini_set('display_errors', 1);
$response=curl_request("http://192.168.1.220/cgi-bin/handle_login.tcl","POST","user=admin&pw=admin&submit=Login");
curl_request("http://192.168.1.220/cgi-bin/controller.tcl?sid=$response&type=inverter&inverter=318002N463");
curl_request("http://192.168.1.220/cgi-bin/overview.tcl?sid=$response&menuParentId=3");
function curl_request($url,$method="GET",$postFields="")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($method=="POST")
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
else
{
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
}
$response = curl_exec($ch);
echo "$response";
return $response;
}
not getting response using curl. I have put all solution but did not get response.
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result); // not display result
this example not displaying any result but it send to specific place.
Add this in your code and then check.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
For your used-case you have to change your code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nhs.uk/organisations/FNM60");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result);
here we are getting response without echo on the top of the page but we don't need to that type of out put so we have disabled using span tag.
echo "<span style='display:none;'>"; //to hide the curl response
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$responseBody = json_decode($response);
print_r($responseBody);
echo $response; // display 1(one)
curl_close($ch);
echo "</span>";
and result is : 1
check full code
This times out every time. How to fix it?
<?
$curl = curl_init("https://gstdealerdaily.fitsvcs.com/siteminderagent/forms/login.fcc");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
You need to add more cURL functions in order to work.
<?php
$target_url="https://gstdealerdaily.fitsvcs.com/siteminderagent/forms/login.fcc";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,4);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$html = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+) ([\"'>]+)#",'$1'.$target_url.'$2$3', $html);
echo $html;
curl_close($ch);
var_dump($ch);
?>
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
print_r($result);
$array_json = json_decode($result, true);
I tried to do a curl to a https site, it say the dns cannot be resolve. But that site I can access it by browser
https://example.com [example]
I did test on my own local installment
$ch = curl_init();
$curl_url = 'https://example.com';
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
print_r($result);
It works correct and print result. I suppose you problem is in this string
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
You could past wrong path or load wrong certificate. I comment this line in my example.
I use file_get_contents function to get and show external links on my specific page.
In my local file everything is okay, but my server doesn't support the file_get_contents function, so I tried to use cURL with the below code:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo file_get_contents_curl('http://google.com');
But it returns a blank page. What is wrong?
try this:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This should work
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://www.google.com";
echo curl_load($url);
I encountered such a problem accessing Google Drive content via the direct link.
After calling file_get_contents returned 302 Moved temporarily
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$html = file_get_contents($url);
echo $html; //print none because error 302.
With the code below it worked again:
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
I tested it today, 03/19/2018
//You can try this . It should work fine.
function curl_tt($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo curl_tt("https://google.com");