简体   繁体   English

Drupal页面向子域站点发送请求(curl):在客户端使用相同的URL时失败

[英]Drupal page sending a request (curl) to a subdomain site: fails while same url works from client

I'm trying to connect two of our websites with each other. 我正在尝试将我们的两个网站相互连接。 First one sends a request to the other one which should reply to it. 第一个向另一个发送请求,该请求应答复。 However, when doing this from backend it fails and from client it works fine. 但是,从后端执行此操作会失败,而从客户端执行此操作会很好。

There's a login required and these parameters are sent within request. 需要登录,并且这些参数在请求内发送。 From debug log I can see that curl follows the different redirections returned from the other site but it always ends up to the login page. 从调试日志中,我可以看到curl遵循从另一个站点返回的不同重定向,但是它始终会到达登录页面。

Is this related to cookies or what? 这与Cookie相关吗? How the backend could get a cookie to be able behave as logged in? 后端如何获得cookie以使其能够像登录时一样运行? Or can I use other cookies under same domain? 还是可以在同一域下使用其他Cookie?

I've tried to use these configurations along with different temp files and variables: curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); 我尝试将这些配置与不同的临时文件和变量一起使用:curl_setopt($ ch,CURLOPT_COOKIEJAR,$ ckfile); curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); curl_setopt($ ch,CURLOPT_COOKIEFILE,$ ckfile);

but these always land on error expect parameter 1 to be resource, null given. 但是这些总是由于错误而出现,期望参数1为资源,给定null。

Please check this 请检查一下

If the cookie is generated from script, then you can send the cookie manually along with the cookie from the file(using cookie-file option). 如果cookie是从脚本生成的,则可以将cookie与文件中的cookie一起手动发送(使用cookie-file选项)。 For example: 例如:

sending manually set cookie 发送手动设置的Cookie

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: test=cookie")); curl_setopt($ ch,CURLOPT_HTTPHEADER,array(“ Cookie:test = cookie”));

sending cookies from file 从文件发送cookie

curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); curl_setopt($ ch,CURLOPT_COOKIEFILE,$ ckfile); In this case curl will send your defined cookie along with the cookies from the file. 在这种情况下,curl将发送您定义的cookie和文件中的cookie。

If the cookie is generated through javascrript, then you have to trace it out how its generated and then you can send it using the above method(through http-header). 如果cookie是通过javascrript生成的,则必须跟踪它的生成方式,然后可以使用上述方法(通过http-header)发送它。

The utma utmc, utmz are seen when cookies are sent from Mozilla. 从Mozilla发送cookie时会看到utma utmc,utmz。 You shouldn't bet worry about these things anymore. 您不应该再为这些事情担心。

Finally, the way you are doing is alright. 最后,您的操作方式还可以。 Just make sure you are using absolute path for the file names(ie /var/dir/cookie.txt) instead of relative one. 只要确保您使用的是文件名的绝对路径(即/var/dir/cookie.txt),而不是相对路径即可。

Always enable the verbose mode when working with curl. 使用curl时,请始终启用详细模式。 It will help you a lot on tracing the requests. 这将对您的跟踪请求有很大帮助。 Also it will save lot of your times. 同样,它将节省您的大量时间。

curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ ch,CURLOPT_VERBOSE,true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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