但不在活动服务器上

但不在活动服务器上

本文介绍了cURL脚本在localhost上工作,但不在活动服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send a SMS through script. I am using curl to run the API sending the SMS on the vendor server.fopen and file_get_contents are blocked on my server. So, cURL is my only option.

脚本: -

// 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响应:

Live server cURL response :

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] => )

Localhost cURL回应:

Localhost cURL response :

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] => )

通过localhost我收到短信,而通过服务器我不是。

Through localhost I am getting an sms whereas through server I am not.

UPDATE -
Localhost php版本:

UPDATE -Localhost php version :

PHP Version 5.4.3

服务器php版本:

PHP Version 5.3.27


推荐答案

很难说,但我建议除了 CURLOPT_TIMEOUT 之外,还要向curl选项添加 CURLOPT_CONNECTTIMEOUT 。我在此示例中将 CURLOPT_CONNECTTIMEOUT 设置为 5 。并将 CURLOPT_HTTPGET 设置为 TRUE 。此外,如果远程服务器需要某个用户代理,请添加 CURLOPT_USERAGENT

This is hard to say, but I would recommend adding CURLOPT_CONNECTTIMEOUT in addition to CURLOPT_TIMEOUT to your curl options. I am setting CURLOPT_CONNECTTIMEOUT to 5 in this example. And set CURLOPT_HTTPGET to TRUE. Also, adding CURLOPT_USERAGENT as well just in case the remote server needs something as a user agent:

// 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版本是否与服务器相同。

Also, unclear if the PHP version on your local setup is the same as the server.

这篇关于cURL脚本在localhost上工作,但不在活动服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:13