卷曲-使用wp_remote_get()的请求响应500,curl_exec响应200 [英] Curl - Request with wp_remote_get() responds 500, curl_exec responds 200

查看:121
本文介绍了卷曲-使用wp_remote_get()的请求响应500,curl_exec响应200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地服务器和生产服务器中都收到奇怪的服务器 php curl错误(Ubuntu 14.04.2 LTS,PHP 5.5.9-1ubuntu4.11,Apache 2.4 .7 )。



基本上,对远程API的curl请求返回状态码500的响应,仅在中wp_remote_get(),它在 curl_exec()和浏览器请求中都返回状态200。



我的调试代码:

 <?php 
$ url = 'https://yoast.com?edd_action=activate_license&license=my-license-key-here&item_name=WooCommerce+Yoast+SEO&url=https://google.com';

//返回状态200:
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
$ result = curl_exec($ ch);
curl_close($ ch);
echo‘< pre>’。 print_r($ result,true)。 ‘< / pre>’;

//返回状态500:
$ testResp = wp_remote_get($ url);
echo‘< pre>’。 print_r($ testResp,true)。 ‘< / pre>’;

我不知道为什么它为 wp_remote_get()。我曾尝试调整传递给 wp_remote_get()的args,但仍使用 500



我还在调试中禁用了所有插件。



有任何想法吗?

解决方案

好,经过一些调试,我相信问题是

wp-includes / class-http.php ,在为 wp_remote_get()创建http请求时设置。



该选项具有过滤器,但默认设置这样创建:

 'user-agent'=> apply_filters('http_headers_useragent','WordPress /'。$ wp_version。';'。get_bloginfo('url')),

因此,在我的情况下,用户代理标头值为: Wordpress / 4.3.1; http://myurl.com



当我进入过滤器 http_headers_useragent 并返回一个空字符串,甚至返回一个不同的用户代理字符串,例如: 'Mozilla / 5.0(Macintosh; Intel Mac OS X 10_8)AppleWebKit / 535.6.2(KHTML,如Gecko)版本/5.2 Safari / 535.6.2',请求将返回成功的200响应。



不确定分号是否是真正的罪魁祸首,但是如果我将其删除并将用户代理字符串设置为 Wordpress / 4.3.1 ,请求也将成功。


I am getting an odd server php curl error in both my local and production servers (Ubuntu 14.04.2 LTS, PHP 5.5.9-1ubuntu4.11, Apache 2.4.7).

Basically, a curl request to a remote API returns a status code 500 response, ONLY in wp_remote_get(), where it returns status 200 in both curl_exec() and a browser request.

My debug code:

<?php
$url = 'https://yoast.com?edd_action=activate_license&license=my-license-key-here&item_name=WooCommerce+Yoast+SEO&url=https://google.com';

// this return status 200:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo '<pre>' . print_r($result, true) . '</pre>';

// this return status 500:
$testResp = wp_remote_get($url);
echo '<pre>' . print_r($testResp, true) . '</pre>';

I cannot figure out why it responds 500 for wp_remote_get(). I've tried adjusting args passed to wp_remote_get(), but still a 500 with it.

I've also disabled all plugins in debugging.

Any Ideas?

解决方案

OK, after a bit of debugging, I believe the issue is the default User-Agent string Wordpress sets in wp-includes/class-http.php, set when creating an http request for wp_remote_get().

The option has a filter, but the default is created like so:

'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ),

So in my case, the 'user-agent' header value was: "Wordpress/4.3.1; http://myurl.com"

When I hook into the filter http_headers_useragent and return an empty string, or even a different user-agent string such as: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/535.6.2 (KHTML, like Gecko) Version/5.2 Safari/535.6.2', the request will return a successful 200 response.

Not sure if the semicolon is the true culprit, but if I remove it and set the user-agent string to just "Wordpress/4.3.1", the request is successful as well.

这篇关于卷曲-使用wp_remote_get()的请求响应500,curl_exec响应200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆