【问题标题】:php file with CURL commands not working on Server带有 CURL 命令的 php 文件在服务器上不起作用
【发布时间】:2015-07-27 15:11:32
【问题描述】:

我有一个包含 curl 代码的 php 文件,以便访问 api。但是当我在服务器上上传这个文件并尝试通过 url 执行它时,没有显示输出。也不显示文件符号,因为它显示为 php 文件。指导我如何解决这个问题。 php文件中的代码是:

 <?php   // set up the curl resource

    $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

    curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch,CURLOPT_POSTFIELDS,"{}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, 'http://184.73.165.170:3000/api/3ace632b194e4366b821f7775b51cc4e/get-ibeacon-contents'); // execute the request

    $output = curl_exec($ch);

    // output the profile information - includes the header

    //echo "curl response is :".($output). PHP_EOL; print($output);

    // close curl resource to free up system resources

    curl_close($ch); ?>

我的服务器索引如图所示。据我了解,php 文件符号与我上传的图像中显示的不同。任何人都可以对此提出建议,如果它可以帮助我解决我的目标。

【问题讨论】:

  • 在 curl_exec 之后,检查 curl_error
  • POSTFIELDS 应该是 url 编码的字符串或数组,我在您的代码中看不到任何 &lt;?php 登录。
  • 我添加 curl_exec 仍然文件没有执行。
  • @NeelIon 请解释 POSTFIELDS 应该是 url 编码的字符串还是数组?我错误地忘记在上面的问题代码中添加
  • 您可以使用key1=value1&amp;key2=value2这种格式发送post参数。

标签: php curl server execution


【解决方案1】:

我在本地尝试过,cURL 似乎可以工作。如果它只在本地而不是在服务器上工作,那么这些是你应该检查的事情:

  • 在服务器 ;extension=php_curl.dll 上启用 cURL 并删除通常在文件 php/php.ini 中的分号; (uncmets)
  • 如果您在 Windows 上本地工作并尝试上传到 Linux 服务器,那么您可能需要更改 EOL for UNIX/OSX 格式。您可以使用 Notepad++ 执行此操作,方法是转到 Edit -&gt; EOL Conversion -&gt; UNIX/OSX Format,然后上传。

【讨论】:

  • CURL 在服务器上启用。我们尝试了 curl 示例文件,它们运行良好。但只有上面的文件不起作用。
  • 参考我上传的有问题的图片,并解释任何查询,请再次阅读。
【解决方案2】:

尝试以这种方式使用您的代码

     <?php

        $json=json_encode(array('key1'=>'val1','key2'=>'val2'));

        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, 'http://184.73.165.170:3000/api/3ace632b194e4366b821f7775b51cc4e/get-ibeacon-contents');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_POST, true); 
        curl_setopt($ch,CURLOPT_POSTFIELDS,$json); 


        $output = curl_exec($ch);

        var_dump(curl_getinfo($ch));
        echo curl_errno($ch) . '-' . curl_error($ch);

        curl_close($ch); 

    ?>

【讨论】:

  • 参考我上传的有问题的图片,并解释任何查询,请再次阅读。
猜你喜欢
  • 2020-04-17
  • 1970-01-01
  • 2012-01-12
  • 2017-02-20
  • 2017-08-16
  • 1970-01-01
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多