douqi3064 2014-06-25 18:49
浏览 26
已采纳

curl命令到php

I want to use this curl command (works with Windows curl)

curl -k -i -H "Accept: application/json" https://brackets-registry.aboutweb.com/registryList 

inside php curl. I tried this code:

<?php
 $url="https://brackets-registry.aboutweb.com/registryList";

 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-type: application/json']);


 $result = curl_exec($ch);
 curl_close($ch);

 var_dump($result);

But I don't get any json only html source code. I think I am using the -H and the -k option, right? Don't know how to add -i.

  • 写回答

3条回答 默认 最新

  • duanchao5258 2014-06-25 19:01
    关注

    Why don't you use a library such as Guzzle to connect to the api?

    In your command line, you use the Accept HTTP header, which negociate the content type of the response, but in your curl option you are using Content-Type, saying any information in your request body is in JSON, nothing about the response type you want. I therefore would have an extra line such as:

    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?