Failed access to webservice ssl through php (curl) - ClientSoap - php

When i use this Php code to access to the wsdl file not secured with authentication basic, it works very well.
<?php
try{
ini_set('soap.wsdl_cache_enabled', false);
$url = 'https://webservicexx:10443/Service.asmx?WSDL';
$wsdl = get_wsdl($url);
$client = new SoapClient($wsdl);
print_r($client);
} catch (SoapFault $e) {
echo $e;
}
function get_wsdl($url) {
clearstatcache();
$cache_file = "/var/www/webservice2/soap.wsdl." . md5($url);
$ch = curl_init($url);
$options = array(
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_FRESH_CONNECT=>true,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($cache_file, $data);
return $cache_file;
}
?>
And when i try to access to the wsdl file secured with authentication basic (.NET), by changing the $option array, to add login and pass:
$options = array(
CURLOPT_POST=>true,
CURLOPT_HTTPAUTH=>CURLAUTH_BASIC,
CURLOPT_USERPWD=>'myLOGIN:myPASSWORD',
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_FRESH_CONNECT=>true,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
I have problems on the file result. I don't get the wsdl file wich is secured, i have the page error on the file, on my server. That means that i don't have access to the file, because the authentication basic barrier, and the login and pass that i added on the $option array doesn't work.
How can i do it ?

Try also setting CURLOPT_SSL_VERIFYHOST to FALSE

Related

Invalid cookies recieved from CURL request and file_get_contents

I am receiving an invalid cookie string when trying capture the cookie using file_get_contents and curl. The cookie received while browsing directly from the browser is valid/active. But, the cookie captured from file_get_contents and curl seems to be invalid.
I am trying to capture from file_get_contents like this
$context = array(
'http' => array(
'method' => 'GET',
'header' => array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8', 'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/63.0.3239.84 Chrome/63.0.3239.84 Safari/537.36'),
)
);
$cxContext = stream_context_create($context);
file_get_contents($url, false, $cxContext);
$cookies = array();
foreach ($http_response_header as $hdr) {
if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
$cookies = $matches[1];
}
}
return $cookies;
I tried playing around with this, by setting headers, but the cookies returned always is either expired or simply invalid.
But, through a browser the cookie I get is always valid.
Anyone faced a similar problem, don't know how to tackle this issue.
There are several unanswered questions from my above comment, but I'll share this bit of code for example purposes. It's what I've used in the past as a base class for browser emulation using cURL:
<?php
if(!function_exists("curl_init")) { throw new Exception("CurlBrowser requires the cURL extension, which is not enabled!"); }
class CurlBrowser
{
public $userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0";
/*
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0
*/
public $cookiesFile = null;
public $proxyURL = null;
public $saveLastOutput = "";
public $caBundle = "cacert.pem";
public $httpHeaders = array();
public function __construct($UseCookies = true)
{
if(is_bool($UseCookies) && $UseCookies)
{
$this->cookiesFile = dirname(__FILE__)."/cookies.txt";
}
elseif(is_string($UseCookies) && ($UseCookies != ""))
{
$this->cookiesFile = $UseCookies;
}
}
public function SetCustomHTTPHeaders($arrHeaders)
{
$this->httpHeaders = $arrHeaders;
}
public function SetProxy($proxy)
{
$this->proxyURL = $proxy;
}
public function Get($url)
{
return $this->_request($url);
}
public function Post($url,$data = array())
{
return $this->_request($url,$data);
}
private function _request($form_url,$data = null)
{
$ch = curl_init($form_url);
// CA bundle
$caBundle = $this->caBundle;
if(file_exists($caBundle))
{
// Detect and convert relative path to absolute path
if(basename($caBundle) == $caBundle)
{
$caBundle = getcwd() . DIRECTORY_SEPARATOR . $caBundle;
}
// Set CA bundle
curl_setopt($ch, CURLOPT_CAINFO, $caBundle);
}
// Cookies
if($this->cookiesFile !== null)
{
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiesFile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiesFile);
}
// User Agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
// Misc
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate");
// Optional proxy
if($this->proxyURL !== null)
{
curl_setopt($ch, CURLOPT_PROXY, $this->proxyURL);
}
// Custom HTTP headers
if(count($this->httpHeaders))
{
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeaders);
}
// POST data
if($data !== null)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
// Run operation
$result = curl_exec($ch);
if($result === false)
{
throw new Exception(curl_error($ch));
}
else
{
if(!empty($this->saveLastOutput))
{
file_put_contents($this->saveLastOutput,$result);
}
return $result;
}
}
}
?>
You'd use it like so:
<?php
$browser = new CurlBrowser();
$html = $browser->Get("https://....");
...etc...
My gut guess is that you're simply missing a cookie jar in your original code, but that's mostly based on gut feeling, since we don't have all your problem code at this time.

If/else for https connection through proxy

I am using this code to check a proxy server, before doing anything else. When the connection is ok, I have a file_get_contents line to connect to a website.
$host = '123.45.678.90';
$port = 80;
$waitTimeoutInSeconds = 1;
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
$aContext = array(
'http' => array(
'proxy' => 'tcp://123.45.678.90:80',
'request_fulluri' => true,
),
);
$cxContext = stream_context_create($aContext);
// connect to website using a proxy server
$file_content = file_get_contents('https://www.anything.com', False, $cxContext);
} else {
// It didn't work
}
fclose($fp);
But though the proxy connection was succesfull, I see this: Warning Cannot connect to HTTPS server through proxy? Is there any chance to have an if/else statement, that allows me to do something if there is no warning and stop doing something if there is a warning? I have looked and googled a lot, but haven't found anything I could try.
Thanks for your help!
try to use curl instead of file_get_contents and it will work
<?php
$url = 'https://www.google.com';
// to check your proxy
// $url = 'http://whatismyipaddress.com/';
$proxy = '50.115.194.97:8080';
// create curl resource
$ch = curl_init();
// set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // read more about HTTPS http://stackoverflow.com/questions/31162706/how-to-scrape-a-ssl-or-https- url/31164409#31164409
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
echo $output;
?>
from PHPhil
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds) doesn't make sense, if this proxy is working, you can simply use:
$aContext = array(
'http' => array(
'proxy' => 'tcp://123.45.678.90:80',
'request_fulluri' => true,
),
);
$cxContext = stream_context_create($aContext);
$file_content = file_get_contents('https://google.com', False, $cxContext);
print $file_content;

PHP curl post always return 500 Internal Server Error

I'm trying to make a php curl to make a post in this link: http://g3cs.uesc.com/dsse1.asp, doing it in browser it works fine, ie: put some code like: 41225295 at input field and click at button it will make a post to http://g3cs.uesc.com/dsse2.asp, my code is:
<?php
function __curl($url,$p=NULL,$h=NULL,$ssl=0,$c=NULL,$tm=690,$header=0)
{
$ch = curl_init();
$opts = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => $header,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => $ssl,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)',
CURLOPT_TIMEOUT => $tm,
CURLOPT_REFERER => 'http://g3cs.uesc.com/dsse1.asp'
);
if($p)
{
$opts[CURLOPT_CUSTOMREQUEST] = 'POST';
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = http_build_query($p);
}
if($h) $opts[CURLOPT_HTTPHEADER] = $h;
if($c) $opts[CURLOPT_COOKIE] = $c;
curl_setopt_array($ch,$opts);
$out = curl_exec($ch);
if(!$out)
{
echo curl_error($ch);
exit(0);
}
curl_close($ch);
return $out;
}
echo __curl('http://g3cs.uesc.com/dsse2.asp',array('opt'=>'0','inacn'=>'127','inref'=>'41225295'));
?>
it always return 500 Internal Server Error.
someone could help me with some hints?
Thanks.
Can you make sure the server-side script (dsse2.asp) has no issues. I strongly believe Status 500 is something related to server-side script. it has nothing to do with your client

cURL headers in command line show content-type as image/png, in PHP shows text/html?

I'm attempting to use cURL to download an external image file. When used from the command line, cURL correctly states the response headers with content-type=image/png. When I attempt to use cURL in PHP however, it returns content-type=text/html.
When attempting to save the file using cURL in PHP, with the CURLOPT_BINARYTRANSFER option set to 1, in conjunction with fopen/fwrite/, the result is a corrupt file.
The only cURL flags I'm using in are -A to send a user agent with the request, which I've also done in PHP by calling curl_setopt($ch, CURLOPT_USERAGENT, ...).
The only thing I can think of that would cause this is perhaps some background request headers sent by cURL which aren't accounted for using the standard PHP functions?
For reference;
CLI
curl -A "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3" -I http://find.icaew.com/data/imgs/736c476534ddf7b249d806d9aa7b9ee8.png
PHP
private function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
$response = array(
'html' => curl_exec($ch),
'http_code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'contentLength' => curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD),
'contentType' => curl_getinfo($ch, CURLINFO_CONTENT_TYPE)
);
curl_close($ch);
return $response;
}
public function parseImage() {
$imageSrc = pq('img.firm-logo')->attr('src');
if (!empty($imageSrc)) {
$newFile = '/Users/firstlast/Desktop/Hashery/test01/imgdump/' . $this->currentListingId . '.png';
$curl = $this->curl('http://find.icaew.com' . $imgSrc);
if ($curl['http_code'] == 200) {
if (file_exists($newFile)) unlink($newFile);
$fp = fopen($newFile,'x');
fwrite($fp, $curl['html']);
fclose($fp);
return $this->currentListingId;
} else {
return 0;
}
} else {
return 0;
}
}
When I mentioned content-type=text/html The call to $this->curl() results in the contentLength and contentType properties of the returned $response variable having the values -1 and text/html respectively.
I can imagine this is quite an obscure question, so I've attempted to provide as much context as to what is going on/what I'm trying to achieve. Any help in understanding why this is the case, and what I can do to resolve/achieve my goal would be greatly appreciated
If you know exactly what you are getting then get_file_contents() is much simpler.
A URL can be used as a filename with this function
http://php.net/manual/en/function.file-get-contents.php
Also, it is helpful to go through the user comments on php.net as they have written many examples and potential issues or tricks to using the function.

Trying to login to site with PHP & cURL?

I've never done something like this before...I'm trying to log into swagbucks.com and get retrieve some information, but it's not working. Can someone tell me what's wrong with my script?
<?php
$pages = array('home' =>
'http://swagbucks.com/?cmd=home',
'login' =>
'http://swagbucks.com/?cmd=sb-login&from=/?cmd=home',
'schedule' =>
'http://swagbucks.com/?cmd=sb-acct-account&display=2');
$ch = curl_init();
//Set options for curl session
$options = array(CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; `rv:1.9.2) Gecko/20100115 Firefox/3.6',`
CURLOPT_HEADER => TRUE,
//CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookies.txt');
//Hit home page for session cookie
$options[CURLOPT_URL] = $pages['home'];
curl_setopt_array($ch, $options);
curl_exec($ch);
//Login
$options[CURLOPT_URL] = $pages['login'];
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala#yahoo.com&pswd=jblake&persist=on';
$options[CURLOPT_FOLLOWLOCATION] = FALSE;
curl_setopt_array($ch, $options);
curl_exec($ch);
//Hit schedule page
$options[CURLOPT_URL] = $pages['schedule'];
curl_setopt_array($ch, $options);
$schedule = curl_exec($ch);
//Output schedule
echo $schedule;
//Close curl session
curl_close($ch);
?>
But it still doesn't log me in. What's wrong?
try to echo each request to see if something went wrong.
(enabling CURLOPT_RETURNTRANSFER)
I suggest you to use
curl_setopt($ch, CURLOPT_COOKIEFILE, '/dev/null');
This way cookies are stored internally in-memory without the need of a separated file.
It works for me with "persist=1" , not "persist=on" :
$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala#yahoo.com&pswd=jblake&persist=on'; // doesn't work
$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala#yahoo.com&pswd=jblake&persist=1'; // works
$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala#yahoo.com&pswd=jblake'; // also works

Categories