2

I am trying to connect to Moz API V2, using HTTP Request by file get contents function but I am new using this... could you guys help me?

Example HTPP Request in their doc:

POST /v2/url_metrics
Host: lsapi.seomoz.com
Content-Length: [length of request payload in bytes]
User-Agent: [user agent string]
Authorization: Basic [credentials]
{
    "targets": ["facebook.com"]
}

Here's the code I am trying:

$url = 'https://lsapi.seomoz.com/v2/url_metrics';
$domains = json_encode(['targets' => 'moz.com']);

$opts = ['http' =>
    [
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded\r\n'.
            ("Authorization: Basic " . base64_encode("mozscape-XXXXX:XXXXX")),
        'content-length' => strlen($domains),
        'user-agent' => $_SERVER['HTTP_USER_AGENT'],
        'content' => $domains,
    ]
];

$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);

print_r($result);

Here is the link of documentation : https://moz.com/help/links-api/making-calls/url-metrics

I got nothing when I print result, Probably I am missing some parameter... :(

Thank you for your time :)

Sophie
  • 410
  • 3
  • 10

2 Answers2

1

Sorry for late solution I forgot to post here before...

Maybe someone is looking for how to use moz API V2 with PHP...

$username='Access ID';
$password='Secret Key';
$URL='https://lsapi.seomoz.com/v2/url_metrics';
$payload = json_encode(array("targets" => ["moz.com"]));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
curl_close ($ch);


print_r(json_decode($result, true));
Sophie
  • 410
  • 3
  • 10
0

Most probably you're simply making an invalid request. You declare the content type as application/x-www-form-urlencoded yet sending the data as application/json.

You also need basic error handling (eg. in case of invalid credentials).

I'd write it this way:

$url = 'https://lsapi.seomoz.com/v2/url_metrics';
$content = json_encode(['targets' => 'moz.com']);

$opts = ['http' => [
    'method' => 'POST',
    'content' => $content,
    'header' => implode("\r\n", [
        'Authorization: Basic ' . base64_encode("mozscape-XXXXX:XXXXX"),
        'Content-Type: application/json',
        'Content-Length: ' . strlen($content),
        'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'],
    ]),
]];

$stream = fopen($url, 'r', false, stream_context_create($opts));

if (!is_resource($stream)) {
    die('The call failed');
}

// header information as well as meta data
// about the stream
var_dump(stream_get_meta_data($stream));

// actual data
var_dump(stream_get_contents($stream));

// free resources
fclose($stream);

To be honest, the sockets & fopen is pretty low level. It would be better for you to use an abstraction layer instead: like Guzzle.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88