Biribu
Biribu

Reputation: 3823

GET curl in php

I have some problems while trying to do a curl to other server from my php script. Both servers are mine.

I have a servlet in java in which I can ask for some events and it works fine if I try to get data from a mobile phone or postman in chrome. It ask for 2 headers, Content-Type and Authorization.

I try to make a client in php:

<?php
    function get($url)
    {
     echo "start get function<br>";
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, array(
     'Content-Type: application/json; charset=utf8',
     'Authorization: Basic data_to_authtenticate_in_base_64'
     ));
     $response = curl_exec($ch);
     echo $response;
     curl_close($ch);
     return $response;
 }
 // Sample call
 echo "start<br>";
 get('MY URL');
 echo "end";
 ?>

With the same data in authorization, I get all information in postman but in php I just get nothing. I don't know how to check complete answer in php to see what is wrong. I have to do a GET. Is it everything ok in my script?

I think this script doesn't reach my other server. I just tested adding some debug lines to the second server and from Postman, it prints them but from php it doesn't. Until now, curl from php has worked fine from this server. I use it a lot in other pages. How can I check if need to activate something?

Upvotes: 1

Views: 397

Answers (1)

Chilion
Chilion

Reputation: 4490

Your code looks ok, so use

curl_setopt($curlhandle, CURLOPT_VERBOSE, true);

It will give you a verbose output of the request, so you exactly see what happens.

== Update

echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';

You'll see what failed during your curl execution with this echo's

Upvotes: 2

Related Questions