简体   繁体   中英

PHP Curl after execution does not return back to Ajax Request

I was integrating a PHP code to ping sitemap to google and Bing. The code executed perfectly but to be a fact if i'm initiating the request from a ajax call. It does not return back

Below is the Jquery code i'm using to make ajax call

$("body").on('click', 'button#sitemap_google_submit', function(event){
    toastr.info("Would require few seconds, Please wait, do not refresh Page..",
                "Submitting Sitemap to Google!",
                {   progressBar:!0,
                    showMethod:"slideDown",
                    hideMethod:"slideUp",
                    timeOut:2e3,
                    preventDuplicates: true,
                    positionClass: "toast-bottom-right"
                }
            );
    $("button#sitemap_google_submit").attr("disabled", true);
    $("button#sitemap_google_submit").html("<i class='ft-loader spinner'></i> &nbsp;Submitting to Google..");

    $.ajax({
            url: '/bypasser/modules/seoController.php',
            type: 'POST',
            dataType: 'JSON',
            data: {type: 'submit', console: 'google'},
        })
        .done(function(resp) {
            $("button#sitemap_google_submit").attr("disabled", false);
            $("button#sitemap_google_submit").html("<i class='fa fa-google fa-lg'></i> &nbsp;Submit to Google");
            toastr[resp.type](resp.message, 
                              resp.heading,
                    {   showMethod:"slideDown",
                        hideMethod:"slideUp",
                        preventDuplicates: true,
                        positionClass: "toast-bottom-right"
                    });
        });
    });

on PHP side i'm using below request

<?php
include("appController.php");

$reqType = preg_replace("/[^a-zA-Z]/", "", $_REQUEST['type']);

class seoController Extends appController
{
    public function submitGoogle($url)
    {
        $url = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".$this->websiteBaseURL().$url;
        $returnCode = $this->myCurl($url);
        if($returnCode == 200) {
            echo json_encode(array("type" => "success",
                                    "heading" => "Success!",
                                    "message" => "Sitemap Submitted to Google!!")
                        );
        }else{
            echo json_encode(array("type" => "warning",
                                    "heading" => "Warning!",
                                    "message" => "Problem occured while submitted SiteMap, try again after Sometime!"
                                )
                            );
        }
    }

    function myCurl($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER , true);  // we want headers
        curl_setopt($ch, CURLOPT_NOBODY, true);  // we don't need body
        curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $httpCode;
    }
}

$seoController = new seoController();

if($reqType == "submit" && 
    preg_replace("/[^a-z]/", "", $_POST['console']) == "google")
        $seoController->submitGoogle("sitemap.xml");
?>

The JSON encodes displays perfectly in the preview panel of network tab of inspect element, but somehow it does not return the response to ajax, what is the issue?

Set

curl_setopt($ch, CURLOPT_HEADER , false);  // we don't want headers

instead of

curl_setopt($ch, CURLOPT_HEADER , true);  // we want headers

we don't want the headers as well, as far ai understand your question, you may be looking for ping status code only!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM