PHP function to connect to pingomatic using cURL - php

I'm creating a PHP function to connect to pingomatic using CURL but the response is always.
Array ( [EXE] => XML-RPC server accepts POST requests only. )
here is my sample code...
function curl_getpage2($url,$data, $referer = null, $agent = null, $header = null, $timeout = 20, $proxy = null, $proxy_username = null, $proxy_password = null) {
//getProxy();
if ($agent == null) {
$agent = getAgent();
}
if ($referer == null) {
$referer = getHost($url);
}
if (!is_array($header)) {
$header = array("Content-Type:text/xml","Host:".getHost($url),"User-Agent:$agent",
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language:en-us,en;q=0.5",
"Accept-Encoding:gzip,deflate",
"Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive:300",
"Connection:keep-alive",
"Cache-Control:max-age=0",
"Content-length: ".strlen($XML));
}
if($proxy == null){
$proxy = "208.100.27.155:60099";
}
if($proxy_username == null && $proxy_password == null){
$proxyUnPW = "unproxy:pwproxy";
}else{
$proxyUnPW = $proxy_username.":".$proxy_password;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD,$proxyUnPW);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
//curl_setopt($ch, CURLOPT_USERAGENT, $agent);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//curl_setopt($ch, CURLOPT_REFERER, $referer);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_AUTOREFERER,TRUE);
//curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
//curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
//curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result['EXE'] = curl_exec($ch);
//$result['INF'] = curl_getinfo($ch);
//$result['ERR'] = curl_error($ch);
curl_close($ch);
return $result;
}
I need some help here. I'm just somewhat new to PHP.

You can submit your site once via the web form and then call a GET request for that url (see
question How to ping automatically to pingomatic in PHP?).
So your URL would be something like the following if you want to ping all services (title and url encoded with urlencode()):
$url = 'http://pingomatic.com/ping/?title='.$title.'&blogurl='.$url.'&rssurl=&chk_weblogscom=on&chk_blogs=on&chk_feedburner=on&chk_newsgator=on&chk_myyahoo=on&chk_pubsubcom=on&chk_blogdigger=on&chk_weblogalot=on&chk_newsisfree=on&chk_topicexchange=on&chk_google=on&chk_tailrank=on&chk_skygrid=on&chk_collecta=on&chk_superfeedr=on');
eg:
http://pingomatic.com/ping/?title=Example%20Title&blogurl=http%3A%2F%2Fwww.example.com%2F&rssurl=&chk_weblogscom=on&chk_blogs=on&chk_feedburner=on&chk_newsgator=on&chk_myyahoo=on&chk_pubsubcom=on&chk_blogdigger=on&chk_weblogalot=on&chk_newsisfree=on&chk_topicexchange=on&chk_google=on&chk_tailrank=on&chk_skygrid=on&chk_collecta=on&chk_superfeedr=on

Related

How to get full html content of specific url?

I used several method to get html content of aptoide.com in php.
1) file_get_contents();
2) readfile();
3) curl as php function
function get_dataa($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Konqueror/4.0; Microsoft Windows) KHTML/4.0.80 (like Gecko)");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
4)PHP Simple HTML DOM Parser
include_once('simple_html_dom.php');
$url="http://aptoide.com";
$html = file_get_html($url);
But all of them give empty output for aptoide.com
Is there a way to get full html content of that url ?
echo file_get_contents('http://www.aptoide.com/'); works fine for me.
So it's possible that aptoide.com has been blocked you. If you want to change your IP (as you said in comment) you have to use this:
$url = 'http://aptoide.com.com/';
$proxy = '127.0.0.1:9095'; // Your proxy
// $proxyauth = 'user:password'; // Proxy authentication if required
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
use your curl get_dataa function with this line added:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
because that page is redirecting to www.aptide.com
full function:
function get_dataa($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Konqueror/4.0; Microsoft Windows) KHTML/4.0.80 (like Gecko)");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

regarding php curl and curl command

Php curl returning blank page for some sites where curl command working fine.
for example : curl www.wikipedia.org generating output but php curl giving blank pages with <html> tags
$ch = curl_init(); // initialize curl with given url
//TBD: all setopt commands return true/false. Should be handled
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
$res = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 0); // stop when it encounters an error
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // iNetClean is web-crawling, no need to verify certificates
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_ENCODING, ""); // accept all encodings - identity, deflate, gzip
// now set the URL
curl_setopt($ch, CURLOPT_URL, $url);
$fp_out = fopen($html_file, 'w');
if (!$fp_out) {
if ($DEBUG) {
error_log("couldn't create " . $html_file);
}
} else {
if ($DEBUG) {
error_log("file created " . $html_file);
}
}
$fp_err = fopen($html_err_file, 'w');
if (!$fp_err) {
if ($DEBUG) {
error_log("couldn't create " . $html_err_file);
}
} else {
if ($DEBUG) {
error_log("error file created " . $html_err_file);
}
}
curl_setopt($ch, CURLOPT_FILE, $fp_out); //rawurlencode($url) . "txt"); // for debugging only
curl_setopt($ch, CURLOPT_STDERR, $fp_err);
$result = curl_exec($ch);
//0 size file is created if no data is downloaded or URL does not exist such as pron00.com. Hence added handler to such errors.
if (#filesize($html_file) > 0) {
//file exists and contain some data
} else {
return false;
}
if ($result == false) {
trigger_error(curl_error($ch));
if ($DEBUG) {
error_log("Curl_exec fail");
}
return false;
}
fclose($fp_out);
fclose($fp_err);
curl_close($ch);
return $result;
You can do this using below code:
<?php
$debug = 1;
$fb_page_url = "http://www.wikipedia.org";
$cookies = 'cookies.txt';
touch($cookies);
$uagent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/36.0.1985.125 Chrome/36.0.1985.125 Safari/537.36';
/**
Get __VIEWSTATE & __EVENTVALIDATION
*/
$ch = curl_init($fb_page_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
$html = curl_exec($ch);
curl_close($ch);
preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);
$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];
/**
Start Fetching process
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fb_page_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 9850);
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
// Collecting all POST fields
$postfields = array();
//$postfields['__EVENTTARGET'] = ""; //this is for further clicking any link
//$postfields['__EVENTARGUMENT'] = ""; //this is for further clicking any link
$postfields['__LASTFOCUS'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['hidStates'] = "";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch); // Get result as fetched web page.
if ($debug) {
echo $ret;
}
curl_close($ch);
?>

Curl not able to get response in same server

I am using curl in my application.
My function in DefaultController:
public function actionWebServices()
{
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$fields='';
foreach ($data as $key => $value)
{
$fields .= $key . '/' . $value . '/';
}
rtrim($fields, '&');
$u='admin';
$p='admin123';
$url='localhost:83/Working-copy/mysite/ServiceTypeMaster/Test';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD, $u.':'.$p);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
session_write_close();
$result = curl_exec($ch);
curl_close($ch);
session_start();
return $result;
}
In serviceTypeMasterController:
public function actionTest()
{
echo "test";
}
I am calling URL of localhost in curl URL. but curl doesn't give any response. It only gives me only Response Doesn't contain any data.
What is wrong?
Typically, that response is if the page/service does not exist. Are you sure you're running your server on port 83 (and not on default 80) and that the url resolves to your actionTest (instead of test) method?

Proxy Authentication Required with cURL

I would like to use a cURL function, but I'm behind a proxy, so I get an HTTP/1.1 407 Proxy Authentication Required error...
This is the php code I use:
$proxy_user = 'Michiel';
$proxy_pass = 'mypassword';
$proxy_url = 'myproxyurl:port';
$proxy = true;
$service_url = "https://www.myapiurltocall.com";
$service_user = 'user:password:FO';
$service_pass = 'password';
$ch = curl_init($service_url);
// Set proxy if necessary
if ($proxy) {
curl_setopt($ch, CURLOPT_PROXY, $proxy_url);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_user.':'.$proxy_pass);
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
}
// Set service authentication
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "{$service_user}:{$service_pass}");
// HTTP headers
$headers['Authorization'] = 'Basic ' . base64_encode("$proxy_user:$proxy_pass");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
And I don't know what I do wrong... How can I get around this error?
This is what I'm mostly using :
function curlFile($url,$proxy_ip,$proxy_port,$loginpassw)
{
//$loginpassw = 'username:password';
//$proxy_ip = '192.168.1.1';
//$proxy_port = '12345';
//$url = 'http://www.domain.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This is what I use and it works perfectly:
$proxy = "1.1.1.1:12121";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$url = "http://www.google.pt/search?q=anonymous";
$credentials = "user:pass"
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD,$credentials);
curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$result=curl_exec ($ch);
curl_close ($ch);
Try to implement this in your function
function send_string($data) {
// pr($data);exit;
$equi_input="Your values";
// echo $equi_input; exit;
$agent = ""//Agent Setting for Netscape
$url = ""; // URL to POST FORM. (Action of Form)
//xml format
$efx_request=strtoupper($equi_input);
//echo $efx_request;exit;
$post_fields = "site_id=""&service_name=""&efx_request=$efx_request";
$credentials = ;
$headers = array("HTTP/1.1",
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . $credentials
);
$fh = fopen('/tmp/test.txt', 'w') or die("can't open file"); //File to write the header information of the curl request.
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_STDERR, $fh);
curl_setopt($ch, CURLOPT_POST, 1); // use this option to Post a form
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); // Pass form Fields.
curl_setopt($ch, CURLOPT_VERBOSE, '1');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free system resources.
fclose($fh);
//echo '<pre>';print_r($array);echo '</pre>';exit;
if (!empty($result)) {
return $result;
} else {
echo "Curl Error" . curl_error($ch);
}
}
check request send 200ok ...
And check your secure certificate if you are using....
Thanks.
Assuming the $proxy_user:$proxy_pass are the correct values, if you are behind an office firewall the authentication credentials to be used would be that of a network administrator and not your own...I've encountered something very similar in my work environment and that was the case for me as my normal credentials didn't have the necessary privileges. The best way to ensure that this is the case however would be to ensure the code works in an open network with your proxy settings commented. Hope this helps!

Posting images via CURL to a form

I've had a tough time trying to find a solution for the following problem, I need to use curl to submit a form on a website, however it also needs to upload a picture its a normal input file field
<input type="file" name="image"/>
I have a class and the curl function is defined like this
function fetch($url, $username='', $data='', $proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
if(isset($proxy)) {
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/3.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
if($username) {
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/{$username}.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/{$username}.txt");
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (is_array($data) && count($data)>0){
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if (is_array($this->headers) && count($this->headers)>0){
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
}
$this->result = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$header_size = $curl_info["header_size"];
$this->headers = substr($this->result, 0, $header_size);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->error = curl_error($ch);
curl_close($ch);
}
Anyone could help me? very lost
If you change the following block of code:
// from
if (is_array($data) && count($data)>0){
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
// to
if (is_array($data) && count($data)>0) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
And to have it post the file uploaded by the user, set up your data array like this:
// make sure $_FILES['file'] has been uploaded and is valid
$data = array('field' => 'value',
'name' => 'test',
'email' => 'something',
'file' => '#' . $_FILES['file']['tmp_name']
);
fetch($url, $username, $data);
That will tell curl to send a form post with a file upload. By setting the post fields to an array and prepending an & to the full path of a file that is a value of the array, curl will send an multipart/form-data post request with your file upload.
See curl file upload example

Categories