file_get_content not working for https in php - php

Has anybody had an issue with display content from a website thats over https? The code was working until all the sites on server got ssl. maybe something to so with the certificate being tlss 1.2? So the site im trying to do this from has this certificate now.
$data = file_get_contents('https://www.ladygaga.com/');
echo $data;

According to php.net
When using SSL, Microsoft IIS will violate the protocol by closing the
connection without sending a close_notify indicator. PHP will report
this as "SSL: Fatal Protocol Error" when you reach the end of the
data. To work around this, the value of error_reporting should be
lowered to a level that does not include warnings. PHP can detect
buggy IIS server software when you open the stream using the https://
wrapper and will suppress the warning. When using fsockopen() to
create an ssl:// socket, the developer is responsible for detecting
and suppressing this warning.
Source link

Based on OpenSSL changes in PHP 5.6, try this:
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents("https://www.ladygaga.com/", false, stream_context_create($arrContextOptions));
echo $response;
Another option would be to use curl as such:
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;
}
$data = file_get_contents_curl("https://www.ladygaga.com");

Related

Posting xml via http using php and curl

I have been following some other examples I found on here including this one: Send an XML post request to a web server with CURL and also some external examples such as http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/
For some reason my code is not working. I am repeatedly getting a response of 0 after a lengthy period of time when I try visiting the php file on the actual server.
The exact same XML returns a response of 200 OK when I test posting it to the same request URL in the Chrome Advance REST Client, so clearly the URL is working.
Can anyone please tell me what is wrong with my PHP code?
<?php
$xml_data ='<cXML version="1.2.005" xml:lang="en-US" payloadID="removedforprivacy" timestamp="2017-05-15T13:00:00.000">'
.'<Header>'
.'<From>'
.'<Credential domain="DUNS">'
.'<Identity>removedforprivacy</Identity>'
.'</Credential>'
.'<Credential domain="CompanyName">'
.'<Identity>removedforprivacy</Identity>'
.'</Credential>'
.'</From>'
.'<To>'
.'<Credential domain="CompanyName">'
.'<Identity>removedforprivacy</Identity>'
.'</Credential>'
.'</To>'
.'<Sender>'
.'<Credential domain="DUNS">'
.'<Identity>removedforprivacy</Identity>'
.'<SharedSecret>removedforprivacy</SharedSecret>'
.'</Credential>'
.'</Sender>'
.'</Header>'
.'<Request deploymentMode="production">'
.'<OrderRequest>'
.'<OrderRequestHeader orderID="999" orderDate="2017-05-08 02:41:17" type="new">'
.'<BillTo>'
.'<Address>'
.'<Name xml:lang="en-US">Nicole Testing</Name>'
.'<PostalAddress name="Nicole Testing">'
.'<DeliverTo>Nicole Testing</DeliverTo>'
.'<Street>1 Test St.</Street>'
.'<Street></Street>'
.'<City>Melbourne</City>'
.'<State>VIC</State>'
.'<PostalCode>3000</PostalCode>'
.'<Country isoCountryCode="AU">AU</Country>'
.'</PostalAddress>'
.'</Address>'
.'</BillTo>'
.'<Comments xml:lang="en-US"></Comments>'
.'</OrderRequestHeader>'
.'<ItemOut lineNumber="1" quantity="1" requestedDeliveryDate="2017-05-20T13:49:32">'
.'<ItemID>'
.'<SupplierPartID>51239929_GC</SupplierPartID>'
.'<SupplierPartAuxiliaryID>Joe Simth</SupplierPartAuxiliaryID>'
.'</ItemID>'
.'<ItemDetail>'
.'<UnitPrice>'
.'<Money currency="AUD">1.32</Money>'
.'</UnitPrice>'
.'<Description xml:lang="en-US">Relaxed Tiger</Description>'
.'<UnitOfMeasure>Landscape Size: 24” x 16”</UnitOfMeasure>'
.'<URL>http://image.url.removed.for.privacy</URL>'
.'<Extrinsic name="quantityMultiplier">1</Extrinsic>'
.'</ItemDetail>'
.'<ShipTo>'
.'<Address addressID="0001">'
.'<Name xml:lang="en-US">Your Name</Name>'
.'<PostalAddress name="">'
.'<DeliverTo>Nicole Testing</DeliverTo>'
.'<Street>1 Test St.</Street>'
.'<Street></Street>'
.'<City>Melbourne</City>'
.'<State>VIC</State>'
.'<PostalCode>3000</PostalCode>'
.'<Country isoCountryCode="AU">AU</Country>'
.'</PostalAddress>'
.'</Address>'
.'</ShipTo>'
.'</ItemOut>'
.'</OrderRequest>'
.'</Request>'
.'</cXML>';
$url = 'http://removed.for.privacy';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); // also tried this with application/xml and same response.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml_data );
$response = curl_exec($ch);
print_r($response);
echo "HTTP response code: ".(int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
It would appear that there is nothing wrong with my code and rather port :8080 was blocked by a firewall. The request URL was using port :8080 so it was returning a status of 0.
I'm not familiar with transferring XML, but it's desirable to use heredocs, in your case it will be:
$xml_data = <<<EOD
<cXML version="1.2.005" xml:lang="en-US" payloadID....
//xml body here
</cXML>
EOD;

PHP: CURL is enabled but has no effect

I'm trying to get data from external website using cURL in PHP but, somehow it's not working.
I've checked out that CURL enable in phpinfo(). It shows cURL is enabled
But, my code is not working.
<?php
if (! function_exists ( 'curl_version' )) {
exit ( "Enable cURL in PHP" );
}
$ch = curl_init ();
$timeout = 0; // 100; // set to zero for no timeout
$myHITurl = "http://www.google.com";
curl_setopt ( $ch, CURLOPT_URL, $myHITurl );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
$file_contents = curl_exec ( $ch );
if (curl_errno ( $ch )) {
echo curl_error ( $ch );
curl_close ( $ch );
exit ();
}
curl_close ( $ch );
// dump output of api if you want during test
echo "$file_contents";
?>
It goes timeout.
I'm not using WAMP or XAMPP server. The above code runs directly on the server.
I've no idea what's going wrong.
Your code is perfect, I have tested it on my own server (data center in Texas) and it worked fine.
My guess is that your server IP is banned. Try to fetch a different URL, and see if it works for you. If it does then you are banned, if it doesn't then it might be a firewall configuration issue in your server.
disable SELinux if you are on Centos or Fedora or any Redhat Distro
nano /etc/selinux/config
Change
SELINUX=enforcing
to
SELINUX=disabled

PHP get request returning false

$data = file_get_contents("http://randomword.setgetgo.com/get.php");
var_dump($data);
I keep getting false when sending this get request, anyone have an idea why?
It works just fine with a simple php script I wrote hosted from the same domain, might that be the issue, how do I go about sending a get request to this API if that is the case?
I tried using curl as well with the same result. It works with my test script but not the API.
As noted in the various comments above, the code originally posted works fine for me but not for the OP - most likely due to a restriction placed upon various standard PHP functions by the webhost. As an alternative, cURL should be able to retrieve the content unless a similar restriction has been placed on standard curl functions too.
$url='http://randomword.setgetgo.com/get.php';
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_USERAGENT, 'curl-wordfetcher' );
$result = curl_exec( $ch );
if( curl_errno( $ch ) ) echo 'Curl error: ' . curl_error( $ch );
curl_close( $ch );
print_r( $result );

error when using cURL

Trying to access the URL https://memphis.ulbranet.com.br/ALEPH/ using cURL in PHP, but it presents the following error:
bool (false) string (80) "error: 14077417: SSL routines: SSL23_GET_SERVER_HELLO: SSLv3 alert illegal parameter"
Anyone know why?
<?php
ini_set( 'display_errors' , true );
error_reporting( E_ALL );
$curl = curl_init();
curl_setopt( $curl , CURLOPT_URL , 'https://memphis.ulbranet.com.br/ALEPH/' );
curl_setopt( $curl , CURLOPT_RETURNTRANSFER , true );
curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false );
var_dump( curl_exec( $curl ) , curl_error( $curl ) );
I'm not sure whether this is a bug in CURL, an incorrectly formatted SSL setup or a version compatibility - but you can fix it by telling CURL to use SSL version 3:
curl_setopt($ch, CURLOPT_SSLVERSION, 3);

Warning: file_get_contents: failed to open stream: Redirection limit reached, aborting

I read over 20 related questions on this site, searched in Google but no use. I'm new to PHP and am using PHP Simple HTML DOM Parser to fetch a URL. While this script works with local test pages, it just won't work with the URL that I need the script for.
Here is the code that I wrote for this, following an example file that came with the PHP Simple DOM parser library:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL');
foreach($html->find('li.name ul#generalListing') as $e)
echo $e->plaintext;
?>
And this is the error message that I get:
Warning: file_get_contents(http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting in /home/content/html/website.in/test/simple_html_dom.php on line 70
Please guide me what should be done to make it work. I'm new so please suggest a way that is simple. While reading other questions and their answers on this site, I tried cURL method to create a handle but I failed to make it work. The cURL method that I tried keeps returning "Resources" or "Objects". I don't know how to pass that to Simple HTML DOM Parser to make $html->find() work properly.
Please help!
Thanks!
Had a similar problem today. I was using CURL and it wasn't returning my any error. Tested with file_get_contents() and I got...
failed to open stream: Redirection limit reached, aborting in
Made a few searches and I'v ended with this function that works on my case...
function getPage ($url) {
$useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36';
$timeout= 120;
$dir = dirname(__FILE__);
$cookie_file = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_ENCODING, "" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_AUTOREFERER, true );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/');
$content = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
else
{
return $content;
}
curl_close($ch);
}
The website was checking for a valid user agent and for cookies.
The cookie issue was causing it! :)
Peace!
Resolved with:
<?php
$context = stream_context_create(
array(
'http' => array(
'max_redirects' => 101
)
)
);
$content = file_get_contents('http://example.org/', false, $context);
?>
You can also inform if you have a proxy in the middle:
$aContext = array('http'=>array('proxy'=>$proxy,'request_fulluri'=>true));
$cxContext = stream_context_create($aContext);
More details on: https://cweiske.de/tagebuch/php-redirection-limit-reached.htm (thanks #jqpATs2w)
Using cURL you would need to have the CURLOPT_RETURNTRANSFER option set to true in order to return the body of the request with call to curl_exec like this:
$url = 'http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// you may set this options if you need to follow redirects. Though I didn't get any in your case
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$content = curl_exec($curl);
curl_close($curl);
$html = str_get_html($content);
I also needed to add this HTTP context options ignore_errors :
see : https://www.php.net/manual/en/context.http.php
$arrContextOptions = array(
"ssl" => array(
// skip error "Failed to enable crypto" + "SSL operation failed with code 1."
"verify_peer" => false,
"verify_peer_name" => false,
),
// skyp error "failed to open stream: operation failed" + "Redirection limit reached"
'http' => array(
'max_redirects' => 101,
'ignore_errors' => '1'
),
);
$file = file_get_contents($file_url, false, stream_context_create($arrContextOptions));
Obviously, I only use it for quick debugging purpose on my local environment. It is not for production.
I'm not sure exactly why you redefined the $html object with a string from get html, The object is meant to be used for searching the string. If you overwrite the object with a string, the object no longer exists and cannot be used.
In any case, to search the string returned from curl.
<?php
$url = 'http://www.example.com/Results.aspx?isa=1&name=A&csz=AL';
include('simple_html_dom.php');
# create object
$html = new simple_html_dom();
#### CURL BLOCK ####
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
# you may set this options if you need to follow redirects.
# Though I didn't get any in your case
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$content = curl_exec($curl);
curl_close($curl);
# note the variable change.
$string = str_get_html($content);
# load the curl string into the object.
$html->load($string);
#### END CURL BLOCK ####
# without the curl block above you would just use this.
$html->load_file($url);
# choose the tag to find, you're not looking for attributes here.
$html->find('a');
# this is looking for anchor tags in the given string.
# you output the attributes contents using the name of the attribute.
echo $html->href;
?>
you might be searching a different tag, the method is the same
# just outputting a different tag attribute
echo $html->class;
echo $html->id;

Categories