I'm haveing trouble opening this url useing fopen
http://sim8430.agni.lindenlab.com:12046/cap/ccab53e3-49d8-c4f8-b3ad-51ff22c1fa17/
however it silently fails for some reason, it opens other urls just fine but no urls like the one above
the fact that it doesnt give an error is not the real problem, the not loading is what im trying to fix
$file = fopen("http://sim8430.agni.lindenlab.com:12046/cap/ccab53e3-49d8-c4f8-b3ad-51ff22c1fa17/", "r") or exit("Unable to open file!");
catch it like this instead
// add extensive logging
error_reporting(E_ALL);
$file = fopen("http://sim8430.agni.lindenlab.com:12046/cap/xyz/", "r");
if($file === false) {
// try through socket open
$file = fsockopen("sim8430.agni.lindenlab.com", 12046, $errno, $errstr);
if($file == false && $errno == 0) die("Socket initialization failed");
else if(!$file) {
die($errstr);
} else {
$out = "GET /cap/xyz/ HTTP/1.1\r\n";
$out .= "Host: sim8430.agni.lindenlab.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($file, $out);
}
}
if($file) {
while (!feof($file)) {
$contents = fgets($file, 128);
}
fclose($file);
}
Always use cURL when reading/parsing a file content that is on the WWW server.
<?php
class CurlTool {
public static $userAgents = array(
'FireFox3' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
'GoogleBot' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'IE7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Netscape' => 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
'Opera' => 'Opera/9.25 (Windows NT 6.0; U; en)'
);
public static $options = array(
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
CURLOPT_AUTOREFERER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_COOKIEJAR => "cookies.txt",
CURLOPT_COOKIEFILE => "cookies.txt",
CURLOPT_SSL_VERIFYPEER => false,
//CURLOPT_COOKIESESSION => false,
);
private static $proxyServers = array();
private static $proxyCount = 0;
private static $currentProxyIndex = 0;
public static $getinfo;
public static function addProxyServer($url) {
self::$proxyServers[] = $url;
++self::$proxyCount;
}
public static function fetchContent($url, $fields = null, $verbose = false) {
//print '*'.$fields.'*';
if (($curl = curl_init($url)) == false) {
throw new Exception("curl_init error for url $url.");
}
if (self::$proxyCount > 0) {
$proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
curl_setopt($curl, CURLOPT_PROXY, $proxy);
if ($verbose === true) {
echo "Reading $url [Proxy: $proxy] ... ";
}
} else if ($verbose === true) {
echo "Reading $url ... ";
}
//$verbose=TRUE;
//print_r($fields);
// debug_print_backtrace();
//url-ify the data for the POST
$fields_string = '';
if (is_array($fields))
foreach ($fields as $key => $value) {
if (empty($key))
continue;
$fields_string .= $key . '=' . urlencode($value) . '&';
if ($verbose === true) {
echo $key . ": " . $value;
}
}
rtrim($fields_string, '&');
if (count($fields) > 0) {
curl_setopt($curl, CURLOPT_POST, count($fields));
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
}
if ($verbose === true) {
echo "Fields string $fields_string ... ";
}
curl_setopt_array($curl, self::$options);
$content = curl_exec($curl);
self::$getinfo = curl_getinfo($curl);
if ($content === false) {
throw new Exception("curl_exec error for url $url " . curl_error($curl));
}
curl_close($curl);
if ($verbose === true) {
echo "Done.\n";
}
$content = preg_replace('#\n+#', ' ', $content);
$content = preg_replace('#\s+#', ' ', $content);
return $content;
}
public static function downloadFile($url, $fileName, $fields = null, $verbose = false) {
if (($curl = curl_init($url)) == false) {
throw new Exception("curl_init error for url $url.");
}
if (self::$proxyCount > 0) {
$proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
curl_setopt($curl, CURLOPT_PROXY, $proxy);
if ($verbose === true) {
echo "Downloading $url [Proxy: $proxy] ... ";
}
} else if ($verbose === true) {
echo "Downloading $url ... ";
}
//url-ify the data for the POST
$fields_string = '';
if (is_array($fields))
foreach ($fields as $key => $value) {
if (empty($key))
continue;
$fields_string .= $key . '=' . urlencode($value) . '&';
}
rtrim($fields_string, '&');
curl_setopt($curl, CURLOPT_POST, count($fields));
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt_array($curl, self::$options);
if (is_file($fileName)) {
$contents = file_get_contents($fileName, false, null, -1, 3 * 1024);
$pattern = "__VIEWSTATE";
if (strpos($contents, $pattern) === false) {
return $fileName;
}
}
// if (is_file($fileName)) {
// // make a HEAD request and try to get the file size HEAD
// // if they differ then redownload the file, otherwise no need
// curl_setopt($curl, CURLOPT_NOBODY, true);
// curl_setopt($curl, CURLOPT_HEADER, true);
// $ret = curl_exec($curl);
// //echo $fileName;
// $size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
// if ($size == filesize($fileName)) {
// return $fileName;
// } else {
// unlink($fileName);
// return self::downloadFile($url, $fileName, $fields, $verbose);
// }
// }
if (substr($fileName, -1) == '/') {
$targetDir = $fileName;
$fileName = tempnam(sys_get_temp_dir(), 'c_');
}
if (($fp = fopen($fileName, "w")) === false) {
throw new Exception("fopen error for filename $fileName");
}
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
$ret = curl_exec($curl);
self::$getinfo = curl_getinfo($curl);
if ($ret === false) {
fclose($fp);
unlink($fileName);
throw new Exception("curl_exec error for url $url.");
} elseif (isset($targetDir)) {
$eurl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
preg_match('#^.*/(.+)$#', $eurl, $match);
fclose($fp);
rename($fileName, "$targetDir{$match[1]}");
$fileName = "$targetDir{$match[1]}";
} else {
fclose($fp);
}
curl_close($curl);
if ($verbose === true) {
echo "Done.\n";
}
return $fileName;
}
}
?>
cURL always send browser headers, and you will not get time out, or be redirected or die effect
download the file or fetch file content, your choice !
Related
I want to make a remote login to my second website but I got some issue because the action file is /user/login.htm and the form is located here /user/index.htm
when I try to make the post on my first website I got the error which says the username and the password are not valid, but I checked few times and everything is ok... the post is something like this:
continueTo=%2Fuser%2Findex.htm&login=&changeLogInPermanentlyPref=true&email=email%40email.com&password=TESTPASS&submit=&logInPermanently=on&_sourcePage=htYSMjoK0IATah86FF3yMT1zDYzaGsbkGTP5awTC0hykcXEpm9ztqjXVz-aUblAsXV9ykLXP1mm4yo_0TuIk1cjwytYy9KBr&__fp=NhVgRkYa2DMUOPQzpwoBNVSbbb9ZJWU7T8w3OeZ9n2BhRFuQKOPeOQ5rYfieNa75&__at=1584900220._Ai-iNOACLcgaLBOUPwPa551GFQRuWOoDqvLmWby2bU.AXG1Vdh-Vj4V25wzYgcpk3G_HvAZ
the script is this :
<?php
session_start();
error_reporting(-1);
include "config.php";
$browser = $_SERVER['HTTP_USER_AGENT'];
$ip = getenv("REMOTE_ADDR");
define('BASE_URL', 'https://www.example.com');
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //set user agent
curl_setopt($curl, CURLOPT_URL, BASE_URL . $_SERVER['REQUEST_URI']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, false); //we need headers so we can see where the redirect is
//forward cookies from user to server
$cookies = array();
foreach ($_COOKIE as $cookie_name => $cookie_value) {
array_push($cookies, $cookie_name . '=' . $cookie_value);
}
$cookies = implode('; ', $cookies);
$cookies = 'Cookie: ' . $cookies;
curl_setopt($curl, CURLOPT_HTTPHEADER, array($cookies));
//forward request type to server
if($_SERVER['REQUEST_METHOD'] === "GET")
{
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
}
else if($_SERVER['REQUEST_METHOD'] === "POST")
{
$post_data = http_build_query($_POST);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
}
pre_special_handle($curl);
$result = curl_exec($curl);
if($result === false)
{
exit('A CURL error occurred: ' . curl_error($curl));
}
$response = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//forward cookies to end user
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item){
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
foreach ($cookies as $cookie_name => $cookie_value){
setcookie($cookie_name, $cookie_value);
}
post_special_handle($curl, $result, $response);
if($response == 302 || $response == 301)
{
$location = get_location($result);
if($location === false)
{
exit('Received a redirect with no location\n\n' . $result);
}
header('Location: ' . $location, true, $response);
curl_close($curl);
exit();
}
echo cleanup_header($result); //Remove the headers and send page to user
curl_close($curl);
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/* Handles special URLs before request */
function pre_special_handle(&$curl)
{
else if(preg_match("/\//", $_SERVER['REQUEST_URI']))
{
}
}
/* Handles special URLs response */
function post_special_handle(&$curl, &$result, $response)
{
if($_SERVER['REQUEST_URI'] == '/login.html')
{
//Do whatever you want after fetching page here...
}
}
//////////////////////////////////////////////////////////////////////////
/* Function to find the redirect location */
function get_location($header)
{
preg_match("/\b[Ll]ocation: ([A-Za-z0-9:\/\._\?=-]+)/", $header, $location_matches);
if(count($location_matches) <= 1)
{
return false;
}
//die($header);
if(strpos($location_matches[1], "http") === false)
{
if(substr($location_matches[1], 0, 1) !== '/') //first char is not /, add it
{
return '/' . $location_matches[1];
}
else
{
return $location_matches[1]; //correct format already, /page.html
}
}
$location_matches[1] = substr($location_matches[1], strlen('https://'));
$first_slash = strpos($location_matches[1], "/");
if($first_slash !== false)
{
return substr($location_matches[1], $first_slash);
}
return false;
}
function cleanup_header($page)
{
$html_start = strpos($page, '<!DOCTYPE');
if($html_start === false)
{
return $page;
}
return substr($page, $html_start);
}
?>
Here is my code to print Json array in table format
<?php include 'header.php' ; ?>
<?php
$base_url = "http://tic.sugarcrmdemo.com/rest/v10";
$username = "sereneintegration";
$password = "Sugar123!";
function call(
$url,
$oauthtoken='',
$type='GET',
$arguments=array(),
$encodeData=true,
$returnHeaders=false
)
{
$type = strtoupper($type);
if ($type == 'GET')
{
$url.= "?" . http_build_query($arguments);
}
$curl_request = curl_init($url);
if ($type == 'POST')
{
curl_setopt($curl_request, CURLOPT_POST, 1);
}
elseif ($type == 'PUT')
{
curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
}
elseif ($type == 'DELETE')
{
curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
}
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
if (!empty($oauthtoken))
{
$token = array("oauth-token: {$oauthtoken}");
curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);
}
if (!empty($arguments) && $type !== 'GET')
{
if ($encodeData)
{
$arguments = json_encode($arguments);
}
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
}
$result = curl_exec($curl_request);
if ($returnHeaders)
{
list($headers, $content) = explode("\r\n\r\n", $result ,2);
foreach (explode("\r\n",$headers) as $header)
{
header($header);
}
return trim($content);
}
curl_close($curl_request);
$response = json_decode($result);
return $response;
}
$url = $base_url . "/oauth2/token";
$oauth2_token_arguments = array(
"grant_type" => "password",
"client_id" => "sugar",
"client_secret" => "",
"username" => $username,
"password" => $password,
"platform" => "base"
);
$oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);
$url = $base_url . "/SREV1_Property/48f10d2c-eca2-9163-77fb-56c7677240e2/link/srev1_property_srev1_unit";
$unit_response = call($url, $oauth2_token_response->access_token, 'GET');
$json = $unit_response;
$jd = json_decode(json_encode($json), 1);
$floors = array();
foreach ($jd->records AS $key => $obj) {
$f = $obj->unit_floor;
$id = $obj->id;
$name = $obj->name;
$suite = $obj->suite;
$sf = $obj->sf;
$floors[$f][$suite]['name'] = $name;
$floors[$f][$suite]['id'] = $id;
$floors[$f][$suite]['sf'] = $sf;
}
//sort floors in desc order
krsort($floors);
foreach($floors as $id => $floor){
ksort($floors[$id]);
}
print '<table class="data-table" >';
foreach($floors as $floor => $suites){
$sqf = 0;
print '<tr>';
print '<td>FLOOR: '.$floor.'</td>';
foreach($suites AS $suite => $value){
$sqf += $value['sf'];
print'<td>Suite:'.$suite.'<br>SF:'.$value['sf'].'</td>';
}
print '<td>'.$sqf.'</td>';
print '</tr>';
}
print '</table>';
?>
But when I try to run this code i am getting an errors like "Trying to get property of non-object " and " Invalid argument supplied for foreach()". Please help me,.
Here is the expected output
I have a few lines of code in a script, and if part A executes, then part B executes TWICE (that is: the cron.php file is added to the crontab TWICE). This is odd, because if I comment out part A, part B only executes ONCE.
Why is this happening? What am I missing?
// Part A
$url = "https://api.sendgrid.com/apiv2/customer.add.json";
$input = "api_user=$sendgrid_user_account&api_key=$sendgrid_master_password&username=$custname.domain.com&website=$sendgridsubuserdomain&password=$sendgridsubusersmtppass&con firm_password=$sendgridsubusersmtppass&first_name=$first&last_name=$last&address=$sendgridsubuseraddress&city=$sendgridsubusercity&state=$sendgridsubuserstate&zip=$ sendgridsubuserzip&email=$email&country=$sendgridsubusercountry&company=$custname&phone=$sendgridsubuserphone";
$sendgrid_output = postCurl($url, $input, false, false);
// Part B
chdir("/etc/nginx/");
exec("2>&1 ./addcron.sh $custname");
Supporting functions:
function postUrl($url, $data, $headers = null, $json_format = true) { // json format is only used if $data is also formatted as a string
$curl_result = postCurl($url, $data);
if($curl_result != false) return $curl_result;
$data_query = http_build_query($data);
$opts = array('http' => array('method' => 'POST', 'content' => $data_query));
if($headers) {
$opts['http']['header'] = $headers;
}
else {
$opts['http']['header'] = "Content-type: application/x-www-form-urlencoded";
}
$st = stream_context_create($opts);
$fp = fopen($url, 'rb', false, $st);
if(!$fp) {
//$result = http_post_fields($url, $data); TODO: add back in once AWS's PHP updated to include http_post_fields function
//if(!empty($result)) return $result;
}
$result = stream_get_contents($fp);
if(empty($result)) {
//$result = http_post_fields($url, $data);
//if(!empty($result)) return $result;
}
return false; // if all else fails, false
}
function postCurl($url, $values, $boolean_return = false, $json_format = true) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
if(is_array($values)) {
$values_string = "";
foreach($values as $key => $value) $values_string .= "$key=$value&";
$values_string = substr($values_string, 0, -1); // remove last "&"
}
else { // if it's not an array, assume JSON
$values_string = $values;
if($json_format == true) {
$input_array = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($values_string));
}
else {
$input_array = array('Content-Length: ' . strlen($values_string));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $input_array);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $values_string);
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
if($boolean_return == false) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
return $server_output;
}
The addcron.sh script:
#!/bin/bash
custname="$1"
(crontab -l; echo "* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php" ) | crontab -
While trying to use cUrl with the Post method in Yii 2, I receive a 400 error code.
Bad Request (#400)
Unable to verify your data submission.
The above error occurred while the Web server was processing your request.
Please contact us if you think this is a server error. Thank you.
This is my code, where i instantiate the CurlTool class:
public function actionSend() {
$model = new \app\models\Licitatie;
if ($model->load(Yii::$app->request->post())) {
$curl_tool = new \common\components\CurlTool();
$result = $curl_tool->fetchContent('http://www.william.ro/licitatia_bursa/frontend/web/index.php/organizator/licitatie/evrika', $model->attributes);
print_r($result);
}
}
public function actionEvrika() {
return json_encode(
array(
'a' => 'b',
)
);
}
this is the curltool class code:
<?php
namespace common\components;
class CurlTool {
public static $userAgents = array(
'FireFox3' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
'GoogleBot' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'IE7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Netscape' => 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
'Opera' => 'Opera/9.25 (Windows NT 6.0; U; en)'
);
public static $options = array(
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
CURLOPT_AUTOREFERER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_COOKIEJAR => "cookies.txt",
CURLOPT_COOKIEFILE => "cookies.txt",
CURLOPT_SSL_VERIFYPEER => false,
//CURLOPT_COOKIESESSION => false,
);
private static $proxyServers = array();
private static $proxyCount = 0;
private static $currentProxyIndex = 0;
public static $getinfo;
public static function addProxyServer($url) {
self::$proxyServers[] = $url;
++self::$proxyCount;
}
public static function fetchContent($url, $fields = null, $verbose = false) {
//print '*'.$fields.'*';
if (($curl = curl_init($url)) == false) {
throw new Exception("curl_init error for url $url.");
}
if (self::$proxyCount > 0) {
$proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
curl_setopt($curl, CURLOPT_PROXY, $proxy);
if ($verbose === true) {
echo "Reading $url [Proxy: $proxy] ... ";
}
} else if ($verbose === true) {
echo "Reading $url ... ";
}
//$verbose=TRUE;
//print_r($fields);
// debug_print_backtrace();
//url-ify the data for the POST
$fields_string = '';
if (is_array($fields))
foreach ($fields as $key => $value) {
if (empty($key))
continue;
$fields_string .= $key . '=' . urlencode($value) . '&';
if ($verbose === true) {
echo $key . ": " . $value;
}
}
rtrim($fields_string, '&');
if (count($fields) > 0) {
curl_setopt($curl, CURLOPT_POST, count($fields));
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
}
if ($verbose === true) {
echo "Fields string $fields_string ... ";
}
curl_setopt_array($curl, self::$options);
$content = curl_exec($curl);
self::$getinfo = curl_getinfo($curl);
if ($content === false) {
throw new Exception("curl_exec error for url $url " . curl_error($curl));
}
curl_close($curl);
if ($verbose === true) {
echo "Done.\n";
}
$content = preg_replace('#\n+#', ' ', $content);
$content = preg_replace('#\s+#', ' ', $content);
return $content;
}
}
class Controller extends \yii\base\Controller
{
/**
* #var boolean whether to enable CSRF validation for the actions in this controller.
* CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true.
*/
public $enableCsrfValidation = false; <- set this to false
...
be careful, i just found out that if this setting is used within the action itself, it might fail;
it might fail within beforeaction;
you can disable csrf in beforeaction
public function beforeAction($action)
{
if($action->id == 'source-in')
{
return true;
}
return parent::beforeAction($action);
}
Is there another way to fix it? Maybe generate a new CSRF token and send it in the POST data?
Okay, this is kinda driving me nuts... I keep receiving an "Internal Server Error" trying to connect to an external FTP connection with Curl. I can access the FTP fine normally, but not through Curl.
I'm using CodeIgniter as a wrapper, but I really don't think that will cause such an issue. I've tried increasing my memory/timeout, but I still can't get in. The 500 internal server error is actually on my page; I can't figure out if Curl is returning anything, but I do know that I just get a normal error through Curl (not an internal server error) if I disable the 'username' or trying to add a 'password' (there is no password for this FTP Login).
Here are my main scripts:
function FTPScrape() {
parent::Controller();
$this->load->model("Curl_m");
}
function index() {
ini_set('memory_limit', '70000000M');
set_time_limit(0);
define('COOKIES_DIR', 'cookies');
define('RANDOM', COOKIES_DIR . '/' . md5(uniqid(mt_rand(), true)));
$this->Curl_m->init(TRUE, RANDOM . '.txt');
$this->Curl_m->start();
$referer = '';
$url = 'ftp://ftp.server.com/';
$str = $this->Curl_m->ftp($url, 'user', '', __line__, $referer);
print "<br><br><textarea cols=\"80\" rows=\"20\">{$str}</textarea><br><br>";
$this->Curl_m->close();
}
Here are the "Curl_m" model functions I use:
function init($cookies = TRUE, $cookie = 'cookies.txt', $compression = '', $proxy = '') {
if(!is_dir('files')) {
mkdir('files', 0777);
}
if(!is_dir('cookies')) {
mkdir('cookies', 0777);
}
else if($dh = opendir('files/')) {
while(($file = readdir($dh)) !== false) {
if(is_file('files/'.$file)) {
unlink('files/'.$file);
}
}
}
$this->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)';
$this->compression = $compression;
$this->proxy = $proxy;
$this->cookies = $cookies;
$this->filenumber = 0;
$this->follow_location = 1;
$this->headers_html = 0;
$this->binary = 0;
if($this->cookies == TRUE) {
$this->cookie($cookie);
}
}
function start() {
$this->process = curl_init();
}
function close() {
curl_close($this->process);
}
function ftp($url, $user = '', $pass = '', $line_no = '', $referer = '') {
return $this->execute($url, '', $line_no, $referer, $user, $pass);
}
function execute($url, $data = '', $line_no = '', $referer = '', $user = '', $pass = '') {
if(isset($this->headers)) {
unset($this->headers);
}
if(preg_match('/\w/xsi', $data)) {
$type = 'POST';
}
else {
$type = 'GET';
}
$host = parse_url($url);
$this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$this->headers[] = 'Accept-Language: en-us,en;q=0.5';
$this->headers[] = 'Accept-Encoding: gzip,deflate';
$this->headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$this->headers[] = 'Keep-Alive: 300';
$this->headers[] = 'Connection: keep-alive';
$this->headers[] = 'Expect:';
if(preg_match('/\w/xsi', $referer)) {
$this->headers[] = 'Referer: ' . $referer . '';
}
if($type == 'POST') {
if(isset($this->Content_type_change)) {
$this->headers[] = 'Content-Type: ' . $this->Content_type_change . '';
unset($this->Content_type_change);
}
else {
$this->headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
if(isset($this->extra_headings)) {
$this->headers[] = $this->extra_headings;
}
if(!preg_match('/https:/xsi', $url)) {
$this->headers[] = 'Content-Length: ' . strlen($data) . '';
}
}
curl_setopt($this->process, CURLOPT_URL, $url);
curl_setopt($this->process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($this->process, CURLOPT_HEADER, $this->headers_html);
curl_setopt($this->process, CURLOPT_USERAGENT, $this->user_agent);
if($this->cookies == TRUE) {
curl_setopt($this->process, CURLOPT_COOKIEFILE, $this->cookie_file);
}
if($this->cookies == TRUE) {
curl_setopt($this->process, CURLOPT_COOKIEJAR, $this->cookie_file);
}
curl_setopt($this->process, CURLOPT_ENCODING, $this->compression);
curl_setopt($this->process, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($this->process, CURLOPT_TIMEOUT, 1200);
curl_setopt($this->process, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->process, CURLOPT_RETURNTRANSFER, 1);
if(strpos("ftp", $url) !== false) {
curl_setopt($this->process, CURLOPT_FTPLISTONLY, 1);
}
if(!empty($user)) {
$usrpwd = $user . ':' . $pass;
curl_setopt($this->process, CURLOPT_USERPWD, $usrpwd);
}
if($this->binary == 1) {
curl_setopt($this->process, CURLOPT_BINARYTRANSFER, 1);
}
if($this->follow_location == 1) {
curl_setopt($this->process, CURLOPT_FOLLOWLOCATION, 1);
}
else {
curl_setopt($this->process, CURLOPT_FOLLOWLOCATION, 0);
}
if($type == 'POST') {
curl_setopt($this->process, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->process, CURLOPT_POST, 1);
}
if(preg_match("/\w/", $this->proxy)) {
curl_setopt($this->process, CURLOPT_PROXY, $this->proxy);
}
$return = curl_exec($this->process);
if($this->file_extension($url,$return) == 'jpg') {
$fh = fopen('captcha.jpg', "w");
fwrite($fh, $return);
fclose($fh);
}
unset($this->headers);
return $return;
}
Does anyone know why I may be having this issue?
Most of the script was created before I started this project (namely the functions in the Curl_m model) I just converted the class into an actual codeigniter model.
If I can figure out how to prevent this from causing an internal server error I should be able to fix the rest easily enough.
Well, I figured out why it wasn't working right. I tried to increase allowed memory and allow unlimited timeout, but I guess my server just didn't want to allow me to do that.
After I spent yesterday and today trying to configure the Amazon EC2 account my client set up so that I could put all of this information on there, I tested the FTP Scraper again and it successfully connected and retrieved the data I was trying to access. My server was really only for testing, anyway--the final script was going to be put on Amazon EC2, but I was putting it off due to the complexity of setting it up (I'd never used Amazon EC2 before).
Either way, the issue was that the script was either timing out or exceeding the allotted memory. By setting it up on a higher-end server I got the connection to work just fine. Apparently logging into FTP with Curl takes more resources/time than I thought it would.