【问题标题】:cURL script working on localhost but not on live servercURL 脚本在本地主机上工作,但不在实时服务器上
【发布时间】:2014-05-26 21:03:41
【问题描述】:

我正在尝试通过脚本发送短信。我正在使用 curl 在供应商服务器上运行发送 SMS 的 API。 fopen 和 file_get_contents 在我的服务器上被阻止。所以,cURL 是我唯一的选择。

脚本:-

// Initialize options for REST interface
$adb_url="http://example.com";
$adb_option_defaults = array(
    CURLOPT_HEADER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 2
  ); 
// ArangoDB REST function.
// Connection are created demand and closed by PHP on exit.
function adb_rest($method,$uri,$query=NULL,$json=NULL,$options=NULL){
    global $adb_url,$adb_handle,$adb_option_defaults;

    // Connect 
    if(!isset($adb_handle)) $adb_handle = curl_init();

    // Compose query
    $options = array(
    CURLOPT_URL => $adb_url.$uri."?".$query,
    CURLOPT_CUSTOMREQUEST => $method, // GET POST PUT PATCH DELETE HEAD OPTIONS 
    CURLOPT_POSTFIELDS => $json,
    CURLOPT_PORT => 8080,
    CURLOPT_HTTPHEADER => Array('Content-type: text/plain')
    ); 
    curl_setopt_array($adb_handle,($options + $adb_option_defaults)); 

    // send request and wait for responce
    $responce =  curl_exec($adb_handle);

    print_r(curl_getinfo($adb_handle));

    return($responce);
}
// Create a collection
$responce = adb_rest("GET","/bulksms/bulksms","username=xxx&password=xxx&type=2&dlr=1&destination=xxx&source=xxx&message=xxxxxx",'');

实时服务器 cURL 响应:

Array ( 
   [url] => http://example.com/bulksms/bulksms?
username=xxx&password=xxx&type=2&dlr=1&destination=xxx&source=xxx&message=xxxxxx 
   [content_type] => 
   [http_code] => 0 
   [header_size] => 0 
   [request_size] => 0 
   [filetime] => -1 
   [ssl_verify_result] => 0 
   [redirect_count] => 0 
   [total_time] => 1.999604 
   [namelookup_time] => 1.315312 
   [connect_time] => 0 
   [pretransfer_time] => 0 
   [size_upload] => 0 
   [size_download] => 0 
   [speed_download] => 0 
   [speed_upload] => 0 
   [download_content_length] => -1
   [upload_content_length] => -1
   [starttransfer_time] => 0 
   [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => )    

本地主机 cURL 响应:

Array (
   [url] => http://example.com/bulksms/bulksms?
username=xxx&password=xxx&type=2&dlr=1&destination=xxx&source=xxx&message=xxxxxx 
   [content_type] => text/plain
   [http_code] => 200
   [header_size] => 129
   [request_size] => 646
   [filetime] => -1
   [ssl_verify_result] => 0
   [redirect_count] => 0
   [total_time] => 0.14 
   [namelookup_time] => 0
   [connect_time] => 0.047
   [pretransfer_time] => 0.047
   [size_upload] => 0
   [size_download] => 54
   [speed_download] => 385
   [speed_upload] => 0 
   [download_content_length] => 54
   [upload_content_length] => 0
   [starttransfer_time] => 0.14
   [redirect_time] => 0
   [certinfo] => Array ( )
   [primary_ip] => xxx.xxx.xxx.xxx 
   [primary_port] => 8080
   [local_ip] => xxx.xxx.x.xxx
   [local_port] => 56359
   [redirect_url] => )    

通过本地主机我收到一条短信,而通过服务器我没有。

更新 - 本地主机 php 版本:

PHP Version 5.4.3

服务器php版本:

PHP Version 5.3.27

【问题讨论】:

  • 如果它不能在本地主机上运行,​​也不能在实时服务器上运行,那么标题中的“但是”是什么意思?对我来说,它读作“卷曲脚本根本不起作用”......
  • 对不起@Michael,刚刚改正了标题
  • 您的 PHP 代码在哪里?请添加代码,以便我们进行调试。我们在这里为您提供帮助,但您必须帮助我们。
  • @JakeGould 更新,添加脚本
  • 刚刚加了一个答案,但是看你的输出,不知道直播服务器上的PHP版本和本地服务器是一样的吗?

标签: php curl


【解决方案1】:

这很难说,但我建议在您的 curl 选项中添加 CURLOPT_CONNECTTIMEOUTCURLOPT_TIMEOUT。在此示例中,我将 CURLOPT_CONNECTTIMEOUT 设置为 5。并将CURLOPT_HTTPGET 设置为TRUE。另外,添加CURLOPT_USERAGENT 以防远程服务器需要某些东西作为用户代理:

// Compose query
$options = array(
CURLOPT_URL => $adb_url.$uri."?".$query,
CURLOPT_CUSTOMREQUEST => $method, // GET POST PUT PATCH DELETE HEAD OPTIONS 
CURLOPT_POSTFIELDS => $json,
CURLOPT_PORT => 8080,
CURLOPT_HTTPGET => TRUE,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
CURLOPT_HTTPHEADER => Array('Content-type: text/plain')
); 
curl_setopt_array($adb_handle,($options + $adb_option_defaults)); 

另外,不清楚您本地设置的 PHP 版本是否与服务器相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 2018-09-12
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多