Wsparcie » Zaawansowane » Zdalny import postów -> cUrl

  • Witam,

    mam aktualnie taki oto problem:

    próbuję zdalnie korzystając z curl’a w php importować wpisy do mojego wordpress’a.

    Kawałek codu php odpowiedzialny za przejscie do kroku drugiego importu :

    function getWpNonceAndMaxFileSize($url) {
    	$tmpWp = curl_init();
    	curl_setopt($tmpWp, CURLOPT_URL, $url);
    	curl_setopt($tmpWp, CURLOPT_HEADER, 0);
    	curl_setopt($tmpWp, CURLOPT_FOLLOWLOCATION, 1);
    	curl_setopt($tmpWp, CURLOPT_POST, 1);
    	curl_setopt($tmpWp, CURLOPT_COOKIEFILE, 'cookies.txt');
    	curl_setopt($tmpWp, CURLOPT_COOKIEJAR, 'cookies.txt');
    	ob_start();
    	curl_exec($tmpWp);
    	if (curl_errno($tmpWp)) {
    		$zwrot = "błąd - " . curl_errno($tmpWp) . ': ' . curl_error($tmpWp);
    	}
    	curl_close($tmpWp);
    	$string = ob_get_contents();
    	ob_end_clean();
    	$start=strpos($string, "name=\"max_file_size\" value=\"") + strlen("name=\"max_file_size\" value=\"");
    	$valueTempMaxFileSize=substr($string, $start);
    	$maxFileSize=substr($valueTempMaxFileSize, 0, strpos($valueTempMaxFileSize, "\" />"));
    
    	$findStr='id="import-upload-form" method="post" action="';
    	$posActionLink=strpos($string, $findStr);
    	$restOfString=substr($string,($posActionLink+strlen($findStr)));
    	$postOfEndUrl=strpos($restOfString,'">');
    	substr($string, $start, $length);
    	$actionUrl=substr($restOfString,0,$postOfEndUrl);
    	$wpNonce=substr($actionUrl, (strpos($actionUrl, "_wpnonce="))+strlen("_wpnonce="));
    	return $wpNonce ? array(
    		"wpnonce"=>$wpNonce,
    		"max_file_size"=>$maxFileSize,
    	) : false;
    }
    $postFields1['action'] = 'save';
    $postFields1['import'] = "@".getcwd()."/dat/blog.xml";
    
    $wpNonceAndMaxFile=getWpNonceAndMaxFileSize("http://adresdomojegobloga/wp-admin/admin.php?import=wordpress");
    
    	if($wpNonceAndMaxFile) {
    		$wpNonce=$wpNonceAndMaxFile["wpnonce"];
    		$postFields1['max_file_size']=$wpNonceAndMaxFile["max_file_size"];
    	}
    	$ch = curl_init();
    	$urlFirstStep="http://" . $url . "/wp-admin/admin.php?import=wordpress&step=1" . ($wpNonce ? "&_wpnonce=".$wpNonce : null);
    	curl_setopt($ch, CURLOPT_URL, $urlFirstStep);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields1);
    	curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla Firefox");
    
    	ob_start();
    	curl_exec($ch);
    	if (curl_errno($ch)) {
    		$zwrot = "błąd - " . curl_errno($ch) . ': ' . curl_error($ch);
    	}
    	curl_close($ch);
    	$string = ob_get_contents();
    	ob_end_clean();
    	$beginActionUrlId=substr($string, strpos($string, 'action="?import=wordpress&step=2&id=')+ strlen('action="?import=wordpress&step=2&id='));
    	$idForActionUrl=substr($beginActionUrlId, 0, strpos($beginActionUrlId, '" method="post">'));
    
    echo $string;
    /**
    * Tutaj wyświetla mi etap drugi : Assign Authors (przypisanie autorów), więc kontynuuję ze swoim skryptem
    **/
    
    $posSecWpNonce=strpos($string, '<input type="hidden" id="_wpnonce" name="_wpnonce" value="')+ strlen('<input type="hidden" id="_wpnonce" name="_wpnonce" value="');
    	$secondString=substr($string, $posSecWpNonce );
    	$secWpnonce=substr($secondString, 0, strpos($secondString, '" /><input type="hidden" name="_wp_http_referer"'));
    
    	$urlSecondStep="http://" . $url . "/wp-admin/admin.php?import=wordpress&step=2&id=" . ($idForActionUrl ? $idForActionUrl : null);
    	$postFields2['_wp_http_referer'] = '/wp-admin/admin.php?import=wordpress&step=1&_wpnonce='.$wpNonce;
    	$postFields2['max_file_size'] = $wpNonceAndMaxFile["max_file_size"];
    	$postFields2['_wpnonce'] = $secWpnonce;
    	$postFields2['author_in[0]'] = '';
    	$postFields2['user_create[0]'] = '';
    	$postFields2['user_select[0]'] = "1";
    //	$postFields2['import'] = "@".getcwd()."/dat/blog.xml";
    //	$postFields2['submit'] = "Submit";
    
    	$nextCh = curl_init();
    	curl_setopt($nextCh, CURLOPT_URL, $urlSecondStep);
    	curl_setopt($nextCh, CURLOPT_HEADER, 0);
    	curl_setopt($nextCh, CURLOPT_FOLLOWLOCATION, 1);
    	curl_setopt($nextCh, CURLOPT_POST, 1);
    	curl_setopt($nextCh, CURLOPT_POSTFIELDS, $postFields2);
    	curl_setopt($nextCh, CURLOPT_COOKIEFILE, 'cookies.txt');
    	curl_setopt($nextCh, CURLOPT_COOKIEJAR, 'cookies.txt');
    	ob_start();
    	curl_exec($nextCh);
    	if (curl_errno($nextCh)) {
    		$zwrot = "błąd - " . curl_errno($nextCh) . ': ' . curl_error($nextCh);
    	}
    	curl_close($nextCh);
    	$string2 = ob_get_contents();
    	ob_end_clean();
    	echo $string2;
    /**
    * Tutaj niestety zamiast podsumowania że dodano odpowiednie wpisy wyświetla mi jeszcze raz pierwszy etap importu :/
    **/

    Co robię źle, czego nie przekazuje przy tym drugim odwołaniu curl’a do $urlSecondStep
    Nie przekazuję jakichś parametrów w poście ?

    Będę wdzięczny za każdą pomoc.

  • Temat ‘Zdalny import postów -> cUrl’ jest zamknięty na nowe odpowiedzi.