Semrush PHP API

iam coding a php API so the problem is that i should walk through uploaded file contain at least 3000 website for the traffic the information needed is like that photo so i just need the organic search the problem is that the search stops before it complete the 3000 websites like in that first photo

and thats my code


$database = "au";
$key = "api key";
$fname = "results-3.csv";

function getValues($domain,$date='') {
    global $database,$key;
    $keywords = $traffic = 0;
    $url  = "$key&display_date={$date}&database={$database}&export_columns=Ot&domain=$domain";

    $reply = trim(@file_get_contents($url));

    // "ERROR 50 :: NOTHING FOUND"
    if (strpos($reply,'ERROR ')===0) {
        return $reply;
    elseif(preg_match('#Organic Traffic\s*(\d+)#s',$reply,$m)) {
        return array($m[1]);

?><!DOCTYPE html>
    <style type="text/css">
        p {
            color: darkslategray;
            margin: 0;

        p.error {
            color: darkgray;

        h3 {
            color: black;
            margin: 1em 0 0.4em 0;

// got file 
if(@$_FILES["infile"]["size"] AND @$_POST['date']) {
		die("Can not move upload file");
	$date= date("Ym",strtotime($_POST['date']))."15.";
		die("Can not open $fname");
	fwrite($csvF, "Domain,$_POST[date] Seo Traffic\n");
	foreach($domains as $domain) {
		echo "<h3>$domain</h3>";
		//$today = getValues($domain);
		$reply = getValues($domain,$date);
        if (is_array($reply)) {
            $domainTraffic = $reply[0];
            echo '<p>Ok: ' . $domainTraffic . '</p>';
        } else {
            $domainTraffic = '';
            echo "<p class=\"error\">{$reply}</p>";

        fwrite($csvF, "$domain,$domainTraffic\n");

	echo "<h1><a href=$fname target=_blank>Download file</a></h1>";
} else {
        <div style="text-align:left;margin-left:20%">
            <form  method="post" enctype="multipart/form-data">
                SE Traffic for  Date entered by user
                Report Date:<input type=text size=12 name=date value="<?php echo date("Y-m-15",strtotime("-10 months"))?>"> (required!)<br>
                File (txt or csv, one domain per line):<input type=file name=infile><br><br>
                <input type=submit value="Start">

Your question is a little confusing dear, would you please explain it a little more?

Tip: if your code works for some result, and stops working after a while, this could be about API usage too. example: The API may block more than 50 serial requests, this is possible.

This is better you ask the support division of API you are using, they would help better.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


thank u so much for ur answer and yes ur right about that its not the code problem its working verywell but after showing the traffic and making between 1100 sites to 1300 it stops for some reason and the code wouldn’t do that right?

thank u that was helpful

That’s it, if code is wrong, you should find by unexpected behaviour, or error, etc… but as you stated it stops working after a while, you should check the error of give nrecord/url you were processing.

Check the url manually(if possible) to make sure the result/output is one as expected, also ask the support about it, maybe they block you becasue of too much serial request(fair, and logical)

Try to log stuffs caused the error, check stuffs at break point, you will find out.

keep goin on great work, happy programming

i checked the url its working well and even when it stops it doesn’t give any error message this is for sure because of two many serial requests thanks again :smiley:

This could be becasue of crash too (example out of memory)! don’t know what is your environment, but this could be possible your app context goes out of memory and crash!

You should check all stuffs and factor and possible issues, but simple, fix is easy I believe.

well the api is semrush and its on the hostgator site so its not environment but that memory thing i don’t have any experience working with and what kind of memory is it

Well one possibility could be becasue of memory stuff dear. maybe API is working without any limitation, and your host is the one limit your work, many stuffs. cannot say anything certain.

Need to check all stuffs, log, messages… to make sure, but possible to fix.

well i read that on the host site they only allow 25 percent of the server cpu for me

Yes, not only CPU, memory, and network usage could stop you!

i read the same for the ram memory on the host site don’t know about the network

I’m not sure, but maybe this level of work is a little complex for you(or your environment), I suggest you start working with partial data for now (e.g. first 100 records), and as you gain experiences, you would go for more heavy work/load.

yes thanks alot ill do it :smiley: