How do I translate this to cURL

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
Seb76
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Oct 05, 2019 2:02 am

Sat Oct 05, 2019 2:16 am

I have a working API request but I have no idea how to translate this into a curl request...

Working:

Code: Select all

GET https://api2.example.com/rest/v2/search/?searchPhrase=abc HTTP/1.1
Host: api2.example.com
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
Authorization: bearer eyJhbGciOiJSUzI1NiIsImtp
I tried translating this into a PHP curl request by using:

Code: Select all

$request_headers=array();
    $request_headers[]= 'Ocp-Apim-Subscription-Key: 1234567890';
    $request_headers[]= 'Authorization: bearer eyJhbGciOiJSUzI1NiIsImtp';
    $handle1=curl_init();
    $api_url='https://api2.example.com/rest/v2/search/?searchPhrase=abc';


    curl_setopt_array(
        $handle1,
        array(
                CURLOPT_URL=>$api_url,
                CURLOPT_POST=>false,
                CURLOPT_RETURNTRANSFER=>true,
                CURLOPT_HTTPHEADER=>$request_headers,
                CURLOPT_SSL_VERIFYPEER=>false,
                CURLOPT_HEADER=>true,
                CURLOPT_TIMEOUT=>-1
        )
    );

    $data=curl_exec($handle1);
But I just get a blank page...
My PHP skills are basic...

Could someone tell me where I went wrong?
Thanks in advance :-)
Post Reply