I tried to validating my XML, but I did not get a proper response, anyone tell me how to validate my post operation? And what is the way of bulk posting with free of category and location?
I have free account in Craigslist. I use this code:
<?php error_reporting(E_ALL);
ini_set('display_errors', '1');
$name='my username';//$_POST['name']; // Get from form or get from DB one per xml
$password= 'my password';//$_POST['password'];
$id='my account ID';//$_POST['id'];
$xmlStart='<?xml version="1.0"?>
<rdf:RDF xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cl="http://www.craigslist.org/about/cl-bulk-ns/1.0">';
$channelStart='<channel>';
$channelItems='<items>';
$channelItemsEnd='</items>';
$channelAuth='<cl:auth username="'.$name.'"
password="'.$password.'"
accountID="'.$id.'"/>';
$channelEnd='</channel>';
$xmlEnd='</rdf:RDF>';
$channelItems .='<rdf:li rdf:resource="$179000 / 3br - 3 Bed/1 Bath Great NE Heights Locating!! No Bank Qualifying!! Owner Fi (NE Albuquerque)"/>';
$items='';
//set the content type to xml
//header("Content-Type: text/xml");
// Initialize the xmlOutput variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xmlBody = '<rdf:RDF xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cl="http://www.craigslist.org/about/cl-bulk-ns/1.0">';
$xmlBody .= "<XML>";
// Run a select query to get my letest 6 items
// Connect to the MySQL database
mysql_connect("localhost","root","");
mysql_select_db('new_listing');
//$dynamicList = "";
$sql = mysql_query("SELECT * FROM listing ORDER BY id");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
// Create channel item string
$channelItems .='';
$id = $row['id'];
$name = $row['username'];
$password= $row['password'];
$address = $row['address'];
$Posting_type = $row['posting_type'];
$category = $row['category'];
$location = $row['location'];
$posting_title = $row['posting_title'];
$specific_loc = $row['specific_loc'];
$postal = $row['postal_code'];
$description = $row['description'];
$reply_to = $row['reply_to'];
$rent = $row['rent'];
$sqft = $row['sqft'];
$bedrooms = $row['bedrooms'];
$bathrooms = $row['bathrooms'];
$housing = $row['housing_type'];
$laundry = $row['laundry'];
$parking = $row['parking'];
$wheelchair = $row['wheelchair'];
$smoking = $row['smoking'];
$furnished = $row['furnished'];
$street = $row['street'];
$cross_street = $row['cross_street'];
$city = $row['city'];
$zip_code = $row['zip_code'];
$date_d = $row['date_d'];
$time_t = $row['time_t'];
$image = $row['images'];
$items .= '
<item rdf:about="real estate">
<cl:category>"housing"</cl:category>
<cl:area>"NE Albuquerque"</cl:area>
<cl:housingInfo price="$179000"
bedrooms="3"
sqft="1400"/>
<cl:replyEmail privacy="C">email address</cl:replyEmail>
<cl:brokerInfo companyName="Your Company name"
feeDisclosure="fee disclosure here" />
<title>"$179000 / 3br - 3 Bed/1 Bath Great NE Heights Locating!! No Bank Qualifying!! Owner Fi (NE Albuquerque)"</title>
<description><![CDATA["3 Bedroom, 1 Bath 1400sqft home with 1 car garage,
Lease With Option To Buy, No Bank Qualifying!!
Price $179,000, $1200 lease per month + $15,000 down
Great Location in NE Heights close to schools, shopping and jobs!!
Very Desirable Location! Call 505-264-7942 and ask for Robert"]]></description>
</item>';
// End while loop
}
mysql_close();
// close the mysql database connection
//$xmlBody .= "</XML>";
$xmlToSubmit=$xmlStart.$channelStart.$channelItems.$channelAuth.$channelItemsEnd.$channelEnd.$items.$xmlEnd;
class cURL {
var $headers;
var $user_agent;
function cURL()
{
$this->headers[] = 'Connection: Keep-Alive';
$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
}
function post($url,$data) {
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_POST, 1);
$return = curl_exec($process);
$info = curl_getinfo($process);
curl_close($process);
return $info;
}
}
$cc = new cURL();
$url = 'https://post.craigslist.org/bulk-rss/post';
$output = $cc->post($url,$xmlToSubmit);
//echo $output;
print_r($output);
} ?>
Related
I have been working on a clients WP site which lists deals from Groupon. I am using the Groupon's official XML feed, importing via WP All Import. This works without much hassle. Now the issue is Groupon doesn't update that feed frequently but some of their deals get sold out or off the market often. So to get this resolved what I am trying is using a CURL script to crawl the links and check if the deal is available or not then turn the unavailable deals to draft posts (Once a day only).
The custom script is working almost perfectly, only after the first 14/24 requests the server starts responding with 502/503 HTTP status codes. To overcome the issue I have used the below precautions -
Using the proper header (captured from the requests made by the browser)
Parsing cookies from response header and sending back.
Using proper referrer and user agent.
Using proxies.
Trying to send request after a set interval. PHP - sleep(5);
Unfortunately, none of this got me the solution I wanted. I am attaching my code and I would like to request your expert insights on the issue, please.
Thanks in advance for your time.
Shahriar
PHP SCRIPT - https://pastebin.com/FF2cNm5q
<?php
// Error supressing and extend maximum execution time
error_reporting(0);
ini_set('max_execution_time', 50000);
// Sitemap URL List
$all_activity_urls = array();
$sitemap_url = array(
'https://www.groupon.de/sitemaps/deals-local0.xml.gz'
);
$cookies = Array();
// looping through sitemap url for scraping activity urls
for ($u = 0; $u < count($sitemap_url); $u++)
{
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0');
curl_setopt($ch1, CURLOPT_REFERER, "https://www.groupon.de/");
curl_setopt($ch1, CURLOPT_TIMEOUT, 40);
// curl_setopt($ch1, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_URL, $sitemap_url[$u]);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
// Parsing Cookie from the response header
curl_setopt($ch1, CURLOPT_HEADERFUNCTION, "curlResponseHeaderCallback");
$activity_url_source = curl_exec($ch1);
$status_code = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
curl_close($ch1);
if ($status_code === 200)
{
// Parsing XML sitemap for activity urls
$activity_url_list = json_decode(json_encode(simplexml_load_string($activity_url_source)));
for ($a = 0; $a < count($activity_url_list->url); $a++)
{
array_push($all_activity_urls, $activity_url_list->url[$a]->loc);
}
}
}
if (count($all_activity_urls) > 0)
{
// URL Loop count
$loop_from = 0;
$loop_to = (count($all_activity_urls) > 0) ? 100 : 0;
// $loop_to = count($all_activity_urls);
$final_data = array();
echo 'script start - ' . date('h:i:s') . "<br>";
for ($u = $loop_from; $u < $loop_to; $u++)
{
//Pull source from webpage
$headers = array(
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-language: en-US,en;q=0.9,bn-BD;q=0.8,bn;q=0.7,it;q=0.6',
'cache-control: max-age=0',
'cookie: ' . implode('; ', $cookies),
'upgrade-insecure-requests: 1',
'user-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
);
$site = $all_activity_urls[$u];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, "https://www.groupon.de/");
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $site);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// Parsing Cookie from the response header
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "curlResponseHeaderCallback");
$data = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status_code === 200)
{
// Ready data for parsing
$document = new DOMDocument();
$document->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $data);
$xpath = new DOMXpath($document);
$title = '';
$availability = '';
$price = '';
$base_price = '';
$link = '';
$image = '';
$link = $all_activity_urls[$u];
// Scraping Availability
$raw_availability = $xpath->query('//div[#data-bhw="DealHighlights"]/div[0]/div/div');
$availability = $raw_availability->item(0)->nodeValue;
// Scraping Title
$raw_title = $xpath->query('//h1[#id="deal-title"]');
$title = $raw_title->item(0)->nodeValue;
// Scraping Price
$raw_price = $xpath->query('//div[#class="price-discount-wrapper"]');
$price = trim(str_replace(array("$", "€", "US", " "), array("", "", "", ""), $raw_price->item(0)->nodeValue));
// Scraping Old Price
$raw_base_price = $xpath->query('//div[contains(#class, "value-source-wrapper")]');
$base_price = trim(str_replace(array("$", "€", "US", " "), array("", "", "", ""), $raw_base_price->item(0)->nodeValue));
// Creating Final Data Array
array_push($final_data, array(
'link' => $link,
'availability' => $availability,
'name' => $title,
'price' => $price,
'baseprice' => $base_price,
'img' => $image,
));
}
else
{
$link = $all_activity_urls[$u];
if ($status_code === 429)
{
$status_msg = ' - Too Many Requests';
}
else
{
$status_msg = '';
}
array_push($final_data, array(
'link' => $link,
'status' => $status_code . $status_msg,
));
}
echo 'before break - ' . date('h:i:s') . "<br>";
sleep(5);
echo 'after break - ' . date('h:i:s') . "<br>";
flush();
}
echo 'script end - ' . date('h:i:s') . "<br>";
// Converting data to XML
$activities = new SimpleXMLElement("<?xml version=\"1.0\"?><activities></activities>");
array_to_xml($final_data, $activities);
$xml_file = $activities->asXML('activities.xml');
if ($xml_file)
{
echo 'XML file have been generated successfully.';
}
else
{
echo 'XML file generation error.';
}
}
else
{
$activities = new SimpleXMLElement("<?xml version=\"1.0\"?><activities></activities>");
$activities->addChild("error", htmlspecialchars("No URL scraped from sitemap. Stoping script."));
$xml_file = $activities->asXML('activities.xml');
if ($xml_file)
{
echo 'XML file have been generated successfully.';
}
else
{
echo 'XML file generation error.';
}
}
// Recursive Function for creating XML Nodes
function array_to_xml($array, &$activities)
{
foreach ($array as $key => $value)
{
if (is_array($value))
{
if (!is_numeric($key))
{
$subnode = $activities->addChild("$key");
array_to_xml($value, $subnode);
}
else
{
$subnode = $activities->addChild("activity");
array_to_xml($value, $subnode);
}
}
else
{
$activities->addChild("$key", htmlspecialchars("$value"));
}
}
}
// Cookie Parsing Function
function curlResponseHeaderCallback($ch, $headerLine)
{
global $cookies;
if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $headerLine, $cookie) == 1)
{
$cookies[] = $cookie[1];
}
return strlen($headerLine); // Needed by curl
}
There is a mess of cookies in your snippet. The callback function just appends cookies to the array regardingless of whether they already exist or not. Here is a new version which at least seems to work in this case since there are no semicolon-seperated multiple cookie definitions. Usually the cookie string should be even parsed. If you have installed the http extension you can use http_parse_cookie.
// Cookie Parsing Function
function curlResponseHeaderCallback($ch, $headerLine)
{
global $cookies;
if (preg_match('/^Set-Cookie:\s*([^;]+)/mi', $headerLine, $match) == 1)
{
if(false !== ($p = strpos($match[1], '=')))
{
$replaced = false;
$cname = substr($match[1], 0, $p+1);
foreach ($cookies as &$cookie)
if(0 === strpos($cookie, $cname))
{
$cookie = $match[1];
$replaced = true;
break;
}
if(!$replaced)
$cookies[] = $match[1];
}
var_dump($cookies);
}
return strlen($headerLine); // Needed by curl
}
i get data with cURL from a database and save it in a variable ($alter).
Next, a text file is read. This is done with a loop a certain number of times. Thereby a line is read from a textfile and written to the variable "$linex". The variable "$linex" should then be compared with the variable "$alter. Then the comparison with the second line takes place and so on.
But unfortunately it doesn't work as described. A "false" is always output for every line, even if the string has to match.
Where is the error in the code?
<?php
header('Content-type: text/html; charset=utf-8');
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';
$id = $_POST["id"];
$id2 = $_POST["klass"];
$id3 = $_POST["element"];
$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/$id2/elements";
$request_headers = [];
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'charset=utf-8';
$request_headers[] = 'Content-Type: application/json; charset=utf-8';
$request_headers[] = 'Accept-Encoding: gzip, deflate, identity';
$request_headers[] = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
$request_headers[] = 'X-picturemaxx-api-key: key';
$request_headers[] = 'Authorization: Bearer key';
$ch = curl_init($url2);
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($result, true);
$alternativnameze = array();
foreach($data['items'] as $alternativ) {
$alternativname = $alternativ['localized'];
$alternativnamez = $alternativname['de-de'];
$alternativnameze[] = $alternativnamez['classification_element_name'];
}
$alter = substr(implode($alternativnameze),0 ,1000000);
$file = 'name_test.txt';
$fh = fopen($file, 'r');
if ($fh === false) {
die('Could not open file: '.$file);
}
for ($i = 0; $i < 6; $i++) {
$linex = fgets($fh);
if (strpos($alter, $linex) !== false) {
echo 'true<br />';
} else {
echo 'false<br />';
}
}
if (fclose($fh) === false) {
die('Could not close file: '.$file);
}
?>
Outoput example for variable "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963], (Clive Staples-Lewis ; Clive
Staples Lewis ; C. S. Luis ; C. Hamilton ; C. S. Ruisu ; Klajv S.
Lʹjuis ; Klaĭv S. Lʹi︠u︡is ; Clive S. Lewis ; Clive Staples Lewis ;
Jack Lewis ; C.S. Luis ; N.W. Clerk ; C.S. Lewis)
Output example for variable "$linex" which should be compared as a substring with the whole string "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963]
Many thanks in advance for all hints and solution suggestions.
Try $linex = rtrim($linex, "\r\n"); prior to the comparison to handle extra line-ending characters which are likely in your file.
fgets returns a line from the file with the new line chararcter. So if a line in your text file is just an a, fgets($fh); will return a\n (in Linux, different character on other OS). strpos("a b c", "a\n") will always returns false.
A trim should do the trick:
$linex = trim(fgets($fh));
recently i upgraded my VPS server so as to increase the ram and storage
After upgrade i noticed that my payment api started giving an error
Error: Illegal characters found in URL
Which i have never encountered before, my API which is used for processing
payments has been running for years and just stopped in march after my upgraded
This is the code am using presently
<?php
/* $myparams = array();
$myparams['subpdtid'] = #### ;
$myparams['submittedref'] = $_SESSION['genref'] ;
$myparams['submittedamt'] = $_SESSION['amt4hash'] ;
$myparams['rettxref'] = $_POST['txnref'] ;
$myparams['retpayRef'] = $_POST['payRef'] ;
$myparams['retretref'] = $_POST['retRef'] ;
$myparams['retcardnum'] = $_POST['cardNum'] ;
$myparams['retappramt'] = $_POST['apprAmt'] ;
$myparams['nhash'] = '########################################################################' ;
*/
$subpdtid = ####; // your product ID
$submittedref = $_POST['tnx_ref']; // unique ref I generated for the trans
$submittedamt = $_POST['amount'];
//$rettxref= $_POST['txnref']; // ref from isw
//$retpayRef=$_POST['payRef']; // pay ref from isw
//$retretref = $_POST['retRef']; // ret ref from isw
//$retcardnum = $_POST['cardNum']; // ret cardnum from isw
//$retappramt = $_POST['apprAmt']; // ret appr amt from isw
$nhash = "04F61D7F7EEE6C80E5D227CA6DAAEA878F46C47B67A9DC7A5A699AD2D13A6249187E85667709F0D978D6697E33817DC771FBA109110ADAD1C4E642D20D439BC2" ; // the mac key sent to you
$hashv = $subpdtid.$submittedref.$nhash; // concatenate the strings for hash again
$thash = hash('sha512',$hashv);
// $credentials = "mithun:mithun";
$parami = array(
"productid"=>$subpdtid,
"transactionreference"=>$submittedref,
"amount"=>$submittedamt
);
$ponmo = http_build_query($parami) . "\n";
//$url = "https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.xml?$ponmo";// xml
$url = "https://webpay.interswitchng.com/paydirect/api/v1/gettransaction.json?$ponmo"; // json
//note the variables appended to the url as get values for these parameters
$headers = array(
"GET /HTTP/1.1",
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
//"Content-type: multipart/form-data",
//"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",
//"Hash:$thash",
"Hash: $thash " );
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,120);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER ,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
}
else {
// Show me the result
// $json = simplexml_load_string($data);
$json = json_decode($data, TRUE);
//var_dump($data);
curl_close($ch);
print_r($json);
//echo ($json['ResponseCode']);
//echo ($json['ResponseDescription']);
// loop through the array nicely for your UI
}?>
<?php session_start();
//if(isset($_POST['name'])){
//$amt1 = htmlspecialchars(mysql_real_escape_string($_POST['amount'])) * 100 ;
$desc = $json['ResponseDescription'];
$_SESSION['desc'] = $desc;
$rep = $json['ResponseCode'];
$_SESSION['rep'] = $rep;
$tref = $_POST['tnx_ref'];
$_SESSION['tref'] = $tref;
$amts = $_POST['amount'] /100;
$_SESSION['amount'] = $amts;
//}
?>
<?php echo ($json['ResponseCode']); ?>
<?php echo ($json ->ResponseDescription);?><div align ="center"><h2 style="color:#090" align="center"> <?php echo ($json['ResponseDescription'] ); ?> </h2><br /><img src="wait.gif" width="200" /><div align ="center"><form action="query.php" method="post" name="niid" id="niid"></form><br /></div>
<script type="text/javascript">
window.onload=function(){
window.setTimeout(function() { document.niid.submit(); }, 3000);
};
</script>
<?php } else {?>
<div align ="center"><h2 style="color:#F00" align="center"> Your Transaction Was Not Successfull<br />Reason: <?php echo ($json['ResponseDescription'] ); ?></h2><br /><img src="wait.gif" width="200" /></div>
Please can anyone enlighten me?
It is because you are appending unwanted \n after string
change your code to
$ponmo = http_build_query($parami);
//$url = "https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.xml?$ponmo";// xml
$url = "https://webpay.interswitchng.com/paydirect/api/v1/gettransaction.json?$ponmo"; // json
i want to parse a website but
i always get an Error: service out of order.
No matter what start or end string i give.
I also tried to use an other URL and i copied
full examples from other users that works for them
but not for me. I also tried to increase the Size to 20000.
But nothing is working.
Here is my php-Script:
<?php
// URL, die durchsucht werden soll
$url = "http://cordis.europa.eu/project/rcn/85400_en.html";
// Zeichenfolge vor relevanten Einträgen
$startstring = "<div class='tech'><p>";
// bis zum nächsten html tag bzw. Zeichenfolge nach relevanten Einträgen
$endstring = "<";
$file = #fopen ($url,"r");
if($file)
{
echo "URL found<br>";
}
if (trim($file) == "") {
echo "Service out of order - File:".$file."<br>";
} else {
$i=0;
while (!feof($file)) {
// Wenn das File entsprechend groß ist, kann es unter Umständen
// notwendig sein, die Zahl 2000 entsprechend zu erhöhen. Im Falle
// eines Buffer-Overflows gibt PHP eine entsprechende Fehlermeldung aus.
$zeile[$i] = fgets($file,20000);
$i++;
}
fclose($file);
}
// Data filtering
for ($j=0;$j<$i;$j++) {
if ($resa = strstr($zeile[$j],$startstring)) {
$resb = str_replace($startstring, "", $resa);
$endstueck = strstr($resb, $endstring);
$resultat .= str_replace($endstueck,"",$resb);
$resultat .= "; ";
}
}
// Data output
echo ("Result = ".$resultat."<br>");
return $resultat;
Any help is appreciate.
thanks in advance
EDIT: The URL is found and file has a value: Resource id #3
Try
<?php
// URL, die durchsucht werden soll
$url = "http://cordis.europa.eu/project/rcn/85400_en.html";
$html = file_get_contents($url);
if ($html === false) {
//Service unavailable
echo 'Service unavailable';
return;
}
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DomXpath($dom);
$div = $xpath->query("//*[#class='tech']")->item(0);
$output = trim($div->textContent);
// Data output
echo ("Result = " . $output. "<br>");
return $output;
Use this it will give expected output.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://cordis.europa.eu/project/rcn/85400_en.html");
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
$headers = array();
$headers[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$headers[] = 'Accept-Encoding:gzip, deflate, sdch';
$headers[] = 'Accept-Language:en-US,en;q=0.8';
$headers[] = 'Cache-Control:max-age=0';
$headers[] = 'Connection:keep-alive';
$headers[] = 'Cookie:CORDIS=14.141.177.158.1441621012200552; PHPSESSID=jrf2e3t4vu56acdkf9np0tat06; WT_FPC=id=14.141.177.158-1441621016.978424:lv=1441605951963:ss=1441604805004
Host:cordis.europa.eu';
$headers[] = 'User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36';
$headers[] = 'Host:cordis.europa.eu';
$headers[] = 'Request URL:http://cordis.europa.eu/project/rcn/85400_en.html';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument;
$dom->loadHTML($server_output);
$xpath = new DomXpath($dom);
$div = $xpath->query("//*[#class='tech']")->item(0);
$data = trim($div->textContent);
echo $data;
?>
Output
I want road map image two addresses, I have tried Google map API and MapQuest API, I want data in static image so that I can use that image to generate PDF file, how can I do this
I have tried
Start address is 6100 Richmond Ave, Houston TX 77057
Google Map
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
$address = $data['address1'].$data['address2']." ".$data['state']." ".$data['city']." ".$data['zip'];
$address = urlencode($address);
$url = "http://maps.googleapis.com/maps/api/staticmap?size=710x300&sensor=true";
$url .= "&path=color:0x0000ff|weight:10|6100+Richmond+Ave+Houston+TX+77057";
$url .= "|".$address ;
$url .= "&markers=color:red|label:A|6100+Richmond+Ave+Houston+TX+77057";
$url .= "&markers=color:green|label:B|".$address;
grab_image($url, $upload_dir.$plan['ID'].".png");
MapQuest Map
First scrapped longitude and latitude each source and destination address
function getLatLong($myaddress) {
$myaddress = urlencode($myaddress);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$myaddress."&sensor=false";
//get the content from the api using file_get_contents
$getmap = file_get_contents_curl($url);
//the result is in json format. To decode it use json_decode
$googlemap = json_decode($getmap);
//get the latitute, longitude from the json result by doing a for loop
foreach($googlemap->results as $res){
$address = $res->geometry;
$latlng = $address->location;
//$formattedaddress = $res->formatted_address;
return $latlng;
}
}
http://open.mapquestapi.com/staticmap/v4/getmap?size=600,200&zoom=14&shapeformat=cmp¢er=40.770021,-73.984003&shape=y_zwFjsrbMxWkz#??}DoC??a#}CyBt#ySiN??fDeP&scenter=40.77069,-73.992378&ecenter=40.770935,-73.97644
for shape check this link
http://www.mapquestapi.com/common/encodedecode.html
How can I get the route via road or we can say driving directions on map in image format.
This is how I have solved above problem
function file_get_contents_curl($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$mapquest_key = "YOUR_API_KEY";
$source_address = "6100 Richmond Ave, Houston TX 77057";
$sLat = "29.73189";
$sLan = "-95.48736";
$plan_add = $plan['address1']." ".$plan['address2']." ".$plan['state']." ".$plan['city']." ".$plan['zip'];
# Step 1: Mapquest API url to get session_key and lng and lat
$mapquest_url1 = "http://www.mapquestapi.com/directions/v1/route?key=".$mapquest_key."&outFormat=json";
$mapquest_url1 .= "&from=".urlencode($wocg_address)."&to=".urlencode($plan_add);
$mapquest_data = file_get_contents_curl($mapquest_url1);
$mapquest_data_array = json_decode($mapquest_data);
$session_id = isset($mapquest_data_array->route->sessionId) ? $mapquest_data_array->route->sessionId : "";
$eLat = isset($mapquest_data_array->route->locations[1]->latLng->lat) ? $mapquest_data_array->route->locations[1]->latLng->lat : "";
$eLan = isset($mapquest_data_array->route->locations[1]->latLng->lng) ? $mapquest_data_array->route->locations[1]->latLng->lng : "";
# Step 2: Get images based on start and end location and session id
if(!empty($session_id) && !empty($eLat) && !empty($eLan)) {
$mapquest_url2 = "http://www.mapquestapi.com/staticmap/v3/getmap?key=".$mapquest_key;
$mapquest_url2 .= "&size=710,300&type=map";
$mapquest_url2 .= "&session=".$session_id;
$mapquest_url2 .= "&scenter=".$sLat.",".$sLan;
$mapquest_url2 .= "&ecenter=".$eLat.",".$eLan;
grab_image($mapquest_url2, $upload_dir.$plan['ID'].".png");
} else {
# If failure with mapquest then scrap image from google map
$plan_address = urlencode($plan_add);
$url = "http://maps.googleapis.com/maps/api/staticmap?size=710x300&sensor=true";
$url .= "&path=color:0x0000ff|weight:10|77001+TX+Houston+USA";
$url .= "|".$plan_address;
$url .= "&markers=color:red|label:A|77001+TX+Houston+USA";
$url .= "&markers=color:green|label:B|".$plan_address;
grab_image($url, $upload_dir.$plan['ID'].".png");
}