Cookie不会与file_get_contents或CURL请求一起发送

时间:2018-08-11 00:31:02

标签: php file-get-contents

我正在尝试向具有POST数据和cookie集的URL发送function handleQuestion(questionElements, index, customerId) { if (questionIndex >= questionElements.length) return; var type = $(this).children().data("question-type"); var isCustomerQuestion = false; switch (type) { case "Name": case "Email": isCustomerQuestion = true; break; } if(isCustomerQuestion) { if (customerId == -1) { $.ajax({ method: "POST", url: urlCustomerCreate, success: function (ajaxData) { handleQuestion(questionElements, questionIndex + 1, ajaxData.NumericValue); } }); } else { // Have ID now handleQuestion(questionElements, questionIndex + 1, customerId); } } } // Go handleQuestion($("#stepSurveyCtnId .questionCtnClass"), 0, -1); 请求。

我的代码是这样的:

file_get_contents

据我所见,这是发布数据,但是在请求期间没有发送$postdata = http_build_query( array( 'search' => 'test', 'token' => '0' ) ); // Create a stream $opts = array( 'http'=> array( 'method'=>"POST", 'content' => $postdata, 'header' => "Content-Type: application/x-www-form-urlencoded\n"."Cookie: session_hash=123456789" ) ); $context = stream_context_create($opts); // Open the file using the HTTP headers set above $file = file_get_contents('https://example.com', false, $context); echo $file; ...

我也尝试过使用相同的请求,但是使用CURL时,我也遇到同样的问题。

任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

标头可能需要用somenumber分隔,而不仅仅是\r\n。您还可以使用数组,它们会被正确发送。

\n