多个cURL请求到Magento

时间:2016-06-06 15:15:51

标签: php magento curl cookies

我必须使用cURL向Magento网站发送3个请求。前两个用于创建cookie并使其在网站中漫游,第三个用于通过POST方法将消息发送到联系表单。 问题是请求似乎不起作用。 这是PHP代码和curl请求信息将遵循:

    <?php

/*
 * On va effectuer 3 requêtes cURL afin de créer le cookie et de 
 * le balader dans le site distant. Une fois le cookie enrichie 
 * par les 2 premières requêtes, on envoi les données en POST
 * au formulaire de contact.
 */

//var_dump($_POST);

//Données
$url = $_POST['url'];
$name = $_POST['msg'][0];
$email = $_POST['msg'][1];
$telephone = $_POST['msg'][2];
$comment = $_POST['msg'][3];
$hideit = '';

//Formatage données formulaire
$curl_datas = array(
    'name' => $name,
    'email' => $email,
    'telephone' => $telephone,
    'comment' => $comment,
    'hideit' => $hideit
);
$curl_datas = http_build_query($curl_datas);

//Gestion Cookie
$cookie = 'cookie.txt';
if(!file_exists(realpath($cookie)))
    touch($cookie); // Si le cookie n'existe pas on le crée


//// Première requête ////

//Initialisation de la connexion
$curl_connection = curl_init($url);

//Définition des options

//Header de la requête
$req_header[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$req_header[] = 'Accept-Encoding:gzip,deflate';
$req_header[] = 'Accept-Language:fr,en;q=0.8';
$req_header[] = 'Cache-Control:no-cache';
$req_header[] = 'Connection:keep-alive';
$req_header[] = 'Host:'.str_replace('/','',str_replace('http://','',$url));
$req_header[] = 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 YaBrowser/14.12.2125.10034 Safari/537.36';

//CURL options
curl_setopt($curl_connection, CURLOPT_HEADER, true);
curl_setopt($curl_connection, CURLINFO_HEADER_OUT, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $req_header);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 25); //limite de temps pour la connexion
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); //True permet de renvoyer la réponse sous forme de string dans une variable
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); //False permet de ne pas retourner d'erreur en cas de connexion SSL non valide
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, true); //Sur 1 pour suivre la redirection "Location" du header de la page distante
curl_setopt($curl_connection, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl_connection, CURLOPT_COOKIESESSION, true);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, realpath($cookie)); // On sauvegarde le cookie

//Exécution de la requête
$result1 = curl_exec($curl_connection);
var_dump(curl_getinfo($curl_connection));
curl_close($curl_connection);


//// Deuxième requête ////
// On enrichie notre cookie avec la page contact

$curl_connection = curl_init($url.'contact/');

$req_header[] = 'Referer:'.$url; // On dit qu'on vient de la page d'accueil afin de leurer magento.

curl_setopt($curl_connection, CURLOPT_HEADER, true);
curl_setopt($curl_connection, CURLINFO_HEADER_OUT, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $req_header);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 25); //limite de temps pour la connexion
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); //True permet de renvoyer la réponse sous forme de string dans une variable
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); //False permet de ne pas retourner d'erreur en cas de connexion SSL non valide
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, true); //Sur 1 pour suivre la redirection "Location" du header de la page distante
curl_setopt($curl_connection, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl_connection, CURLOPT_COOKIESESSION, true);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, realpath($cookie)); // On sauvegarde le cookie

$result2 = curl_exec($curl_connection);
var_dump(curl_getinfo($curl_connection));
curl_close($curl_connection);

//// Troisième requête ////
// On envoi les données au formulaire de contact

$curl_connection = curl_init($url.'contact/index/post/');

$req_header2[] ='Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$req_header2[] ='Accept-Encoding:gzip,deflate';
$req_header2[] ='Accept-Language:fr,en;q=0.8';
$req_header2[] ='Cache-Control:no-cache';
$req_header2[] ='Connection:keep-alive';
$req_header2[] ='Host:'.str_replace('/','',str_replace('http://','',$url));
$req_header2[] ='Origin:'.$url;
$req_header2[] ='Referer:'.$url.'contact/'; // On dit qu'on vient du formulaire.
$req_header2[] ='User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 YaBrowser/14.12.2125.10034 Safari/537.36';

curl_setopt($curl_connection, CURLOPT_HEADER, true);
curl_setopt($curl_connection, CURLINFO_HEADER_OUT, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $req_header2);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 25); //limite de temps pour la connexion
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); //True permet de renvoyer la réponse sous forme de string dans une variable
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); //False permet de ne pas retourner d'erreur en cas de connexion SSL non valide
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, true); //Sur 1 pour suivre la redirection "Location" du header de la page distante
curl_setopt($curl_connection, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl_connection, CURLOPT_POST, true); // On va envoyer du POST
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $curl_datas); // On envoie les champs du formulaire initial
curl_setopt($curl_connection, CURLOPT_COOKIESESSION, true);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, realpath($cookie)); // On sauvegarde le cookie

$result3 = curl_exec($curl_connection);
var_dump(curl_getinfo($curl_connection));
curl_close($curl_connection);

//var_dump(curl_getinfo($curl_connection));
echo json_encode($result3);
echo $result1 . $result2 . $result3;

?>

和请求回复:

array (size=27)
  'url' => string 'https://www.venroy.com.au/?SID=pp0j3808nggc4kn7idor5843o3' (length=57)
  'content_type' => string 'text/html; charset=UTF-8' (length=24)
  'http_code' => int 302
  'header_size' => int 6930
  'request_size' => int 5016
  'filetime' => int -1
  'ssl_verify_result' => int 19
  'redirect_count' => int 10
  'total_time' => float 5.943
  'namelookup_time' => float 5.522
  'connect_time' => float 5.522
  'pretransfer_time' => float 5.522
  'size_upload' => float 0
  'size_download' => float 25
  'speed_download' => float 4
  'speed_upload' => float 0
  'download_content_length' => float 25
  'upload_content_length' => float 0
  'starttransfer_time' => float 5.943
  'redirect_time' => float 5.522
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '54.79.75.34' (length=11)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 443
  'local_ip' => string '192.168.1.53' (length=12)
  'local_port' => int 52650
  'request_header' => string 'GET /?SID=pp0j3808nggc4kn7idor5843o3 HTTP/1.1
Cookie: PHPSESSID=pp0j3808nggc4kn7idor5843o3
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:fr,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:https:www.venroy.com.au
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 YaBrowser/14.12.2125.10034 Safari/537.36

' (length=463)
array (size=27)
  'url' => string 'https://www.venroy.com.au/contact/?SID=tgqcahsh306nkhgfbp60hinbj1' (length=65)
  'content_type' => string 'text/html; charset=UTF-8' (length=24)
  'http_code' => int 302
  'header_size' => int 7018
  'request_size' => int 5500
  'filetime' => int -1
  'ssl_verify_result' => int 19
  'redirect_count' => int 10
  'total_time' => float 5.897
  'namelookup_time' => float 5.492
  'connect_time' => float 5.492
  'pretransfer_time' => float 5.492
  'size_upload' => float 0
  'size_download' => float 25
  'speed_download' => float 4
  'speed_upload' => float 0
  'download_content_length' => float 25
  'upload_content_length' => float 0
  'starttransfer_time' => float 5.897
  'redirect_time' => float 5.492
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '54.79.75.34' (length=11)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 443
  'local_ip' => string '192.168.1.53' (length=12)
  'local_port' => int 52676
  'request_header' => string 'GET /contact/?SID=tgqcahsh306nkhgfbp60hinbj1 HTTP/1.1
Cookie: PHPSESSID=tgqcahsh306nkhgfbp60hinbj1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:fr,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:https:www.venroy.com.au
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 YaBrowser/14.12.2125.10034 Safari/537.36
Referer:https://www.venroy.com.au/

' (length=507)
array (size=27)
  'url' => string 'https://www.venroy.com.au/contact/index/?SID=232knnhjjng7i8aelj17h4u0p6' (length=71)
  'content_type' => string 'text/html; charset=UTF-8' (length=24)
  'http_code' => int 302
  'header_size' => int 7324
  'request_size' => int 7006
  'filetime' => int -1
  'ssl_verify_result' => int 19
  'redirect_count' => int 10
  'total_time' => float 6.147
  'namelookup_time' => float 5.725
  'connect_time' => float 5.725
  'pretransfer_time' => float 5.725
  'size_upload' => float 0
  'size_download' => float 25
  'speed_download' => float 4
  'speed_upload' => float 0
  'download_content_length' => float 25
  'upload_content_length' => float 0
  'starttransfer_time' => float 6.147
  'redirect_time' => float 5.725
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '54.79.75.34' (length=11)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 443
  'local_ip' => string '192.168.1.53' (length=12)
  'local_port' => int 52677
  'request_header' => string 'GET /contact/index/?SID=232knnhjjng7i8aelj17h4u0p6 HTTP/1.1
Cookie: PHPSESSID=232knnhjjng7i8aelj17h4u0p6; message_box_display=1; private_content_version=3d6eea71bd829b652b11e871ab617fbf
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:fr,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:https:www.venroy.com.au
Origin:https://www.venroy.com.au/
Referer:https://www.venroy.com.au/contact
User-Agent:Mozilla/5.0 (Win'... (length=636)
false

感谢大家是否可以帮助我理解请求响应,以及为什么它返回false,因此似乎没有用。

祝你有个美好的一天:)

编辑:我认为我有所有请求的重定向循环,因为卷曲错误返回47.我怎么能避免它?谢谢

0 个答案:

没有答案