Scrape Magento网站使用Cookies进行"添加到购物车"

时间:2016-02-04 13:13:19

标签: php magento curl cookies

我在抓this URL

我想要刮掉这个项目的数量。

为此,我的策略是我会尝试添加数量并POST表单,直到出现错误"" xyz product"还不够。"

我尝试使用file_get_contents();echo输出张贴FORM,它会返回一个错误页面,上面写着 Cookies must be enabled in your browser

以下是我发布的方式

    $postdata = http_build_query(
            array(
                'product' => $prod_id,
                'qty' => 5,
                'related_product' => ''
            )
    );

    $opts = array('http' =>
        array(
            'method' => 'POST',
            'header' => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context = stream_context_create($opts);

    $result = file_get_contents($action_url, false, $context);

我还尝试了cURL

$ cookieFile = tempnam(null,' SMS');         $ userAgent =' Mozilla / 5.0(X11; Ubuntu; Linux x86_64; rv:11.0)Gecko / 20100101 Firefox / 11.0&#39 ;;

    $cookieFile = tempnam(null, 'SMS');
    $userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $action_url);

    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 0);
    $post = array(
        'product' => $prod_id,
        'qty' => 5,
        'related_product' => '');

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    $html = curl_exec($ch);

但我仍然从该网站得到错误,应该在浏览器中启用Cookie。

所以我的问题是如何使用PHP将产品添加到购物车?

1 个答案:

答案 0 :(得分:0)

我终于用 Imports System.DirectoryServices Module Module1 Sub Main() Dim run As Boolean = True Dim Filter As String While run Console.WriteLine("Enter Filter:") Filter = Console.ReadLine() If Filter = "exit" Then run = False Else checkFilter(Filter) End If End While End Sub Function checkFilter(Filter As String) As Boolean Dim search As New DirectorySearcher("LDAP://dc=Domain,dc=com") Try search.Filter = Filter search.PropertiesToLoad.Add("name") search.PropertiesToLoad.Add("distinguishedName") search.SearchScope = SearchScope.Subtree Dim results As SearchResultCollection = search.FindAll() If results Is Nothing Then Console.WriteLine("Nothing") Return False Else If results.Count() = 0 Then Console.WriteLine("non found") End If Dim result As SearchResult For Each result In results Console.WriteLine(result.Properties("name")(0).ToString()) Console.WriteLine(result.Properties("distinguishedName")(0).ToString()) 'For Each prop In result.Properties("members") ' Console.WriteLine(prop.ToString()) 'Next Next Console.WriteLine(String.Format("{0} Users Found", results.Count())) End If Catch ex As Exception Console.WriteLine(ex.Message) End Try Return True End Function End Module

做了

它接收任何服务器发送的cookie。